It is very convenient to use C # To create a thread. A Void function can create its own thread application with a statement. See the following example:
Simple thread Creation
public class ThreadController
{
public ThreadController()
{
System.Threading.Thread thread = new System.Threading.Thread(ThreadMethod);
thread.Start();
}
private void ThreadMethod()
{
//Do something
}
}
However, the preceding example shows that the ThreadMethod method cannot input parameters. To solve this problem, ParameterizedThreadStart is required. Let's look at this example:
Parameter thread
public class ThreadController
{
public ThreadController()
{
int value = 10;
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ThreadMethod));
thread.Start(value);
}
private void ThreadMethod(object parameter)
{
Console.WriteLine(parameter.ToString());
}
}
In this way, the thread response function can receive parameters. There is also a problem to note here, that is, the parameter passed in as the parameter. Although no Ref is added to the parameter description, it is actually a reference of the transfer address. Therefore, if parameter is modified in ThreadMethod. The value in ThreadController is also modified.