For thread:
1. Using ThreadStart
Static void Main (string[] args) { Thread th1=new Thread (new ThreadStart (SayHello)); Th1. Start ();
Console.readkey ();}
Static void SayHello () { Console.WriteLine ("hello! " );}
This form can only be an empty type function without parameters, and if you need to pass arguments, you have to use Parameterizedthreadstart.
2. Using Parameterizedthreadstart
Static voidMain (string[] args) {Thread Th1=NewThread (NewParameterizedthreadstart (SayHello)); stringPara="I ' m parameter!"; Th1. Start (para); //incoming parameter paraConsole.readkey ();}Static voidSayHello (Objectobj) //Can only be object type, only one, multiple parameters need to be encapsulated in a custom object { stringstr = obj as string; Console.WriteLine (str);}
This method is still an empty type, it allows us to pass a parameter, but only the object type, can only pass one.
In the thread pool:
1. Call null type method without argument
Static void Main (string[] args) { ThreadPool.QueueUserWorkItem (new// This method can be shortened into ThreadPool.QueueUserWorkItem (SayHello); Console.readkey ();} Static void SayHello () { Console.WriteLine ("hello! " );}
2. Calling an empty type method with parameters
Static voidMain (string[] args) { stringPara="I ' m parameter!"; ThreadPool.QueueUserWorkItem (NewWaitCallback (SayHello), para);//write the parameters here, this method can be abbreviated to ThreadPool.QueueUserWorkItem (Sayhello,para);Th1. Start (para);//incoming parameter paraConsole.readkey ();}Static voidSayHello (ObjectMB ()//can only be of type object, only one, multiple parameters need to be encapsulated in a custom object{ stringstr = obj as string; Console.WriteLine (str);}
This method, like Parameterizedthreadstart, can only pass a parameter of type object.
You should use the ThreadPool class when you reach the following goals:
1. To create and delete threads in the simplest way;
2. The performance of the application using threading is a priority.
You should use the thread class when you reach the following goals:
1. To control the priority of the created thread;
2. You want the thread that you are using to maintain its identity, which is performed in various ways with the thread, over many different time periods;
3. The thread used has a longer lifespan.
Multi-shadow clone--c# using three threads (calling methods and parameters)