. NET defines the function of multithreading in the System.Threading namespace. Therefore, to use multiple threads, you must first declare a reference to this namespace (using System.Threading;).
Even if you do not have the experience of writing multithreaded applications, you may have heard the words "Start thread" "Kill thread", in addition to these two, there are also multithreading aspects such as "pause thread" "Priority" "Suspend thread" "Recovery thread" and so on. Below will be an explanation of one.
A. Start a thread
As the name suggests, "Start a thread" is to create a new and start a thread meaning, the following code can be implemented:
Thread thread1 = new Thread(new ThreadStart( Count));
The Count is the function that will be executed by the new thread.
B. Killing threads
"Kill a Thread" is to destroy the first thread, in order not to waste effort, before killing one of the threads, it is best to determine whether it is still alive (through the IsAlive property), and then can call the Abort method to kill the thread.
C. Pausing a thread
It means to keep a running thread dormant for a period of time. such as thread. Sleep (1000); is to keep the thread dormant for 1 seconds.
D. Priority
There's no need to explain this. The thread class has a ThreadPriority property that is used to set the priority, but does not guarantee that the operating system will accept the priority. The priority of a thread can be divided into 5 kinds: Normal, AboveNormal, BelowNormal, highest, Lowest. The specific implementation examples are as follows:
thread.Priority = ThreadPriority.Highest;
E. Suspending Threads
The suspend method of the thread class is used to suspend the thread, knowing that the resume is invoked before the thread can continue. If the thread is already suspended, it will not work.
if (thread.ThreadState = ThreadState.Running)
{
thread.Suspend();
}
F. Recovery threads
Used to recover a thread that has been suspended so that it continues to execute, and it will not work if the thread is not suspended.
if (thread.ThreadState = ThreadState.Suspended)
{
thread.Resume();
}