- Threadstart does not need to pass parameters or return parameters.
We know that the most intuitive way to start a thread is to use the Thread class. The specific steps are as follows:
InstanceCode:
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 );
}
We have definedThreadstart type DelegationThis delegate develops the method to be executed by the thread: Calculate, in which the area of a circle with a diameter of 0.5 is calculated and output. this constitutes the simplest example of multithreading. In many cases, this is enough, and the threadstart delegate is defined as void threadstart (), that is, the method to be executed does not have parameters. This is obviously a big deficiency. To make up for this defect, it is smart.ProgramThe employee came up with many good methods. We will introduce them in the section that needs to pass multiple parameters. Here we will introduce them first. net another delegate set to solve this problem: parameterizedthreadstart, which will be detailed below.
- parameterizedthreadstart A single parameter needs to be passed
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
instance code:
parameterizedthreadstart threadstart = new parameterizedthreadstart (calculate)
thread = new thread ()
thread. start (0.9);
Public void calculate (Object Arg)
{< br> 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, it is still of the object type, type conversion is still required for use, but it is good to have parameters. By combining multiple parameters into a class, the instance of this class is passed as a parameter, multiple parameters can be passed