. Net defines multithreading in the system. Threading namespace. Therefore, to use multithreading, you must first declare and reference this namespace (using system. Threading ;).
Even if you have not compiled a multi-threaded ApplicationProgramExperience, you may have heard of the words "starting a thread" and "killing a thread". In fact, apart from these two words, multithreading is also involved, such as "suspending Threads", "Priority", "suspending Threads", and "restoring Threads. One by one.
A. Start the thread
As the name suggests, "starting a thread" means creating and starting a thread, as follows:CodeImplementation:
Thread thread1 = new thread (New threadstart (count ));
The count is the function to be executed by the new thread.
B. Killing threads
"Killing a thread" is to remove a thread from the root. In order not to waste effort, it is best to determine whether it is still alive (through the isalive attribute) before killing a thread ), then you can call the abort method to kill this thread.
C. Pause the thread
It means to sleep a running thread for a period of time. For example, thread. Sleep (1000) is used to sleep the thread for 1 second.
D. Priority
There is no need to explain this. The thread class has a threadpriority attribute, which is used to set the priority, but the operating system cannot accept this priority. A thread has five priorities: Normal, abovenormal, belownormal, highest, and lowest. The specific implementation example is as follows:
Thread. Priority = threadpriority. Highest;
E. Thread Suspension
The suspend method of the thread class is used to suspend a thread. This thread can continue to execute until it knows to call resume. If the thread has been suspended, it will not work.
If (thread. threadstate = threadstate. Running)
{
Thread. Suspend ();
}
F. Restore the thread
It is used to restore a suspended thread to continue execution. It does not work if the thread is not suspended.
If (thread. threadstate = threadstate. Suspended)
{
Thread. Resume ();
}
The following is an example to illustrate the simple thread processing function. This example is from the help documentation.
using system;
using system. threading;
// simple threading scenario: Start a static method running
// on a second thread.
public class threadexample {
// The threadproc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
Public static void threadproc () {
for (INT I = 0; I <10; I ++) {
console. writeline ("threadproc: {0}", I);
// yield the rest of the time slice.
thread. sleep (0);
}< BR >}
Public static void main (){
Console. writeline ("main thread: Start a second thread .");
// The constructor for the Thread class requires a threadstart
// Delegate that represents the method to be executed on
// Thread. C # simplifies the creation of this delegate.
Thread t = new thread (New threadstart (threadproc ));
// Start threadproc. On a uniprocessor, the thread does not get
// Any processor time until the main thread yields. uncomment
// The thread. Sleep that follows T. Start () to see the difference.
T. Start ();
// Thread. Sleep (0 );
For (INT I = 0; I <4; I ++ ){
Console. writeline ("main thread: Do some work .");
Thread. Sleep (0 );
}
Console. writeline ("main thread: Call join (), to wait until threadproc ends .");
T. Join ();
Console. writeline ("main thread: threadproc. Join has returned. Press enter to end program .");
Console. Readline ();
}
}
The output of this Code is similar to the following:
Main thread: Start a second thread.
Main thread: do some work.
Threadproc: 0
Main thread: do some work.
Threadproc: 1
Main thread: do some work.
Threadproc: 2
Main thread: do some work.
Threadproc: 3
Main thread: Call join (), to wait until threadproc ends.
Threadproc: 4
Threadproc: 5
Threadproc: 6
Threadproc: 7
Threadproc: 8
Threadproc: 9
Main thread: threadproc. Join has returned. Press enter to end program.