C # supports executing code in parallel through multithreading, where a thread has its own execution path and can run concurrently with other threads. A C # program starts with a single thread, which is created automatically by the CLR and the operating system, and has multithreading to create additional threads.
Unless specified, all examples assume that the following namespaces are referenced:
Using System.Threading;
This should be added to the reference, otherwise the error 、。。。
Try a wireless output x and Y program with the result:
The output does not stop until the window is closed
In the startup of the thread:
Instantiating the thread needs to provide a delegate that is used when instantiating the delegate, which is the method to run when the thread is started in the future.
There are two ways to start a thread in. NET, one is to start without parameters, and the other is to start with a parameter.
1. Starting mode without parameters:
If no additional information is required to start the parameter, you can use ThreadStart to instantiate the thread:
The output is:
2. Starting method with parameters
If you want to take some arguments when instantiating a thread, you cannot instantiate the thread with the ThreadStart delegate as the argument to the constructor, but Parameterizedthreadstart the delegate.
It is also the method to be executed when the thread starts, but it can be instantiated with a method with an object parameter as a constructor parameter, and the method used to instantiate the ThreadStart is no parameter ...
Here, when implemented, defines the pause interval of the program, which enables the control of the interval between threads during operation:
The output is:
C # (7)---thread