VS2005 C # multi threading: how to start 10 threads

What is the best way to start 10 threads, as in a for loop for an intensive process. Sample code would be very helpful.

for (int i = 0; i<=10; i++) //10 threads are intended.
{
    LongRunningMethod();
}
+3
source share
6 answers

ThreadPool.QueueUserWorkItem

+5
source

Or, if you do not want to use the thread pool, just create and start a new thread.

new Thread( delegate() { LongRunningMethod(); } ).Start();
+2
source

Action .

Action newThread = new Action(LongRunningMethod);

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

.NET 3.5, .NET 2.0

public delegate void NoParametersMethod();

NoParametersMethodnewThread = new NoParametersMethod(LongRunningMethod);

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

, , , , LongRunningMethod() Threadpool. ThreadPool, ( , ).

Thread 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();
}

1: ParameterizedThreadStart.
2: < 100 -, . .

MSDN:

, :

  • .
  • , .
  • , . , .
  • . ThreadPool .
  • , , .

, MSDN , " ". , >= 0,5 , . Thread . .

+1

- ...

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();
}
0

Source: https://habr.com/ru/post/1716032/


All Articles