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}}