ThreadStart is different from ParameterizedThreadStart.

Source: Internet
Author: User

ThreadStart is different from ParameterizedThreadStart.

ThreadStart:

ThreadStart is defined as void ThreadStart (). That is to say, the method to be executed cannot have parameters.

ThreadStart threadStart=new ThreadStart(Calculate);Thread thread=new Thread(threadStart);thread.Start();public void Calculate()   { double Diameter=0.5; Console.Write("The Area Of Circle with a Diameter of {0} is {1}"Diameter,Diameter*Math.PI);   }

Here we defineThreadStart type DelegationThis delegate develops the method to be executed by the thread: Calculate. In this method, calculates the circumference of a circle with a diameter of 0.5 and outputs the result. this constitutes the simplest example of multithreading, which is sufficient in many cases.


 

ParameterThreadStart:

ParameterThreadStart is defined as void ParameterizedThreadStart (object state). The startup function of the thread defined by this delegate can accept an input parameter. The example is as follows:

 

ParameterizedThreadStart threadStart=new ParameterizedThreadStart(Calculate)Thread thread=new Thread() ;thread.Start(0.9);public void Calculate(object arg){double Diameter=double(arg);Console.Write("The Area Of Circle with a Diameter of {0} is {1}"Diameter,Diameter*Math.PI);}


The Calculate method has a parameter of the object type. Although there is only one parameter and it is of the object type, type conversion is still required when using it, but fortunately there is a parameter, by combining multiple parameters into a class, and passing the instance of the class as a parameter, multiple parameters can be passed. for example:

 

 

class AddParams{    public int a, b;    public AddParams(int numb1, int numb2)    {      a = numb1;      b = numb2;    }}#endregionclass Program{    static void Main(string[] args)    {      Console.WriteLine("***** Adding with Thread objects *****");      Console.WriteLine("ID of thread in Main(): {0}",        Thread.CurrentThread.ManagedThreadId);      AddParams ap = new AddParams(10, 10);      Thread t = new Thread(new ParameterizedThreadStart(Add));      t.Start(ap);      Console.ReadLine();    }    #region Add method    static void Add(object data)    {      if (data is AddParams)      {        Console.WriteLine("ID of thread in Main(): {0}",          Thread.CurrentThread.ManagedThreadId);        AddParams ap = (AddParams)data;        Console.WriteLine("{0} + {1} is {2}",          ap.a, ap.b, ap.a + ap.b);      }    }    #endregion}}


 


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.