Multi-shadow clone--c# using three threads (calling methods and parameters)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.