views:

340

answers:

5

What is the best way to launch 10 threads like in a for loop for a process intensive method. A code example would be very helpful.

for (int i = 0; i<=10; i++) //10 threads are intended.
{
    LongRunningMethod();
}
+1  A: 

Use Action as a delegate to create a new thread for each long running method call.

Action newThread = new Action(LongRunningMethod);

// Call Async which is a new thread
newThread.BeginInvoke(null, null);

Action with no template is only in .NET 3.5, for .NET 2.0 you have to declare a delegate

public delegate void NoParametersMethod();

NoParametersMethodnewThread = new NoParametersMethod(LongRunningMethod);

// Call Async which is a new thread
newThread.BeginInvoke(null, null);
David Basarab
Or use an existing one such as System.Threading.ThreadStart
KeeperOfTheSoul
Delegate.BeginInvoke() utitizes the Remoting Messaging system and incurs a fair overhead, using ThreadPool.QueueUserWorkItem avoids this overhead
STW
+3  A: 

ThreadPool.QueueUserWorkItem

P.K
Just make sure you have enough MaxThreads in there (10, in this case), otherwise some of your threads will wait in the queue.
Noam Gal
The thread pool is shared between your app and the CLR library. There are scenarios where using it might have negative effect on the app.
Franci Penov
We can't really tell what LongRunningMethod is doing, but given the name it is probably not optimal to use thread pool threads for this.
Brian Rasmussen
And you can even have more than 10 thread but only 10 will run at a time.
Jonathan Shepherd
+2  A: 

Or if you don't want to use the thread pool, just create and start a new thread.

new Thread( delegate() { LongRunningMethod(); } ).Start();
KeeperOfTheSoul
just inquisitive...any reason for not using the ThreadPool?
P.K
Using thread pool threads for long running jobs can have implications on your app and the CLR performance.
Franci Penov
Reasons for not using the thread pool are described under "Limitations of Using the Thread Pool"http://www.beansoftware.com/ASP.NET-Tutorials/Multithreading-Thread-Pool.aspx
Harv
The ThreadPool recycle's threads, there-by avoiding the overhead of creating new threads. As Franci says, the main reason to avoid it is for long-running background threads--in which case spawning a new thread will incur the initial overhead, but leave the pool threads free for short-job's to use.
STW
The OP mentions VS2005, aka C#2. So a lambda isn't helpful.
Henk Holterman
Ah forgot that, changed to C# delegate syntax
KeeperOfTheSoul
+2  A: 

As mentioned in the other answers, you can use ThreadPool.QueueUserWorkItem to schedule your job on a thread in the thread pool or create a new explicit Thread instance for that work. You can read more about the various ways to create threads at the MSDN Threading Tutorial.

Which approach you use can have certain implications on the performance and behavior of your app and the CLR. For more details, read When to use thread pool in C#

Franci Penov
A: 

Something simple like this ...

var threads    = new List<Thread>();
var numThreads = 10;

for( int i = 0; i < numThreads; i++ )
{
  threads.Add( new Thread( () => DoWork() ) );
  threads[i].Name = i.ToString();
  threads[i].Start();
}
JP Alioto