ThreadStart:
ThreadStart This delegate is defined as void ThreadStart (), which means that the method being 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 define a ThreadStart type delegate that defines the method the thread needs to execute: Calculate, in this method, calculates the circumference of a circle with a diameter of 0.5 and outputs it. This constitutes the simplest example of multithreading, In many cases, that's enough.
Parameterthreadstart:
Parameterthreadstart is defined as void Parameterizedthreadstart (object state), the start function of the thread defined by this delegate can accept an input parameter, 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 type object, and although there is only one argument, and it is of type object, a type conversion is still required, but it is good to have parameters, and by combining multiple parameters into a class and then passing an instance of the class as a parameter, You can implement multiple parameter passes. For example:
Class Addparams
{public
int A, b;
Public addparams (int numb1, int numb2)
{
a = NUMB1;
b = numb2;
}
}
#endregion
class 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 (a);
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
}
}