3

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();
}
M. Tibbits
  • 8,400
  • 8
  • 44
  • 59
dotnet-practitioner
  • 13,968
  • 36
  • 127
  • 200

6 Answers6

5

ThreadPool.QueueUserWorkItem

P.K
  • 18,587
  • 11
  • 45
  • 51
2

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

new Thread( delegate() { LongRunningMethod(); } ).Start();
Chris Chilvers
  • 6,429
  • 3
  • 32
  • 53
  • 2
    Using thread pool threads for long running jobs can have implications on your app and the CLR performance. – Franci Penov Aug 25 '09 at 19:25
  • 1
    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 Aug 25 '09 at 19:26
  • 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 Aug 25 '09 at 19:27
  • 2
    The OP mentions VS2005, aka C#2. So a lambda isn't helpful. – H H Aug 25 '09 at 19:39
  • Ah forgot that, changed to C# delegate syntax – Chris Chilvers Aug 25 '09 at 19:41
2

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#

Community
  • 1
  • 1
Franci Penov
  • 74,861
  • 18
  • 132
  • 169
1

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
  • 72,212
  • 42
  • 129
  • 156
  • 1
    Delegate.BeginInvoke() utitizes the Remoting Messaging system and incurs a fair overhead, using ThreadPool.QueueUserWorkItem avoids this overhead – STW Aug 25 '09 at 19:19
1

The answers and comments here may seem a bit divided, this is because the first thing to do here is to check if your LongRunningMethod() is suited for the Threadpool. If it is then using the ThreadPool is the best choice (low overhead, some rudimentary loadbalancing).

Otherwise, use Thread and ThreadStart:

ThreadStart starter = new ThreadStart(LongRun);

for (int i = 1; i <= 10; i++) // 10 threads are intended, note the i=1
{    
    Thread t = new Thread(starter);
    t.Start();
}

Note 1: that there also exists a ParameterizedThreadStart class.
Note 2: You can't scale this to i < 100 or something, you need to keep a lid on the max number of threads. The pool already does that.

From MSDN:

When Not to Use Thread Pool Threads

There are several scenarios in which it is appropriate to create and manage your own threads instead of using thread pool threads:

  • You require a foreground thread.
  • You require a thread to have a particular priority.
  • You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large number of blocked thread pool threads might prevent tasks from starting.
  • You need to place threads into a single-threaded apartment. All ThreadPool threads are in the multithreaded apartment.
  • You need to have a stable identity associated with the thread, or to dedicate a thread to a task.

Note that MSDN does not say what a 'long period of time' is. I'll take a stab at it and say that when your task takes >= 0.5 second you might start looking at separate threads. The overhead of creating a Thread becomes insignificant. But it depends on a lot of factors.

H H
  • 263,252
  • 30
  • 330
  • 514
0

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
  • 44,864
  • 6
  • 88
  • 112