As I have been working on a silver-electric networking project some time ago, I have used multithreading and some basic technologies based on the underlying socket communication. I will take a look at it and put it here. The basic programming points and steps of socket have been posted above. Here we will continue to post the basic knowledge of multithreading.
Define namespace
In. net, The multithreading function is defined in the system. Threading namespace.
Using System. threading;
Start thread
The thread class in the system. Threading namespace represents a thread object. This class object can be used to create new threads, delete, pause, and recover threads.
The followingCodeUse the Thread class to create a new thread and then start the thread:
ThreadMythread;
Mythread = new thread (New threadstart (writedata ));
Mythread. Start ();
Writedata is a function to be executed by this thread. The Code is as follows:
Protected void writedata (){
String STR;
For (INT I = 0; I <= 10000; I ++ ){
STR = "secondary thread" + I. tostring ();
Console. writeline (listview1.listitems. Count, STR, 0, new string [] {""});
Update ();
}
}
Kill thread
The abort method of the thread class is used to permanently kill a thread. However, before calling the abort method, you must determine whether the thread is still activated ,:
If (mythread. isalive)
{
Mythread. Abort ();
}
Pause a thread
The thread. Sleep method is used to pause a thread for a period of time. The Code is as follows:
Mythread. Sleep (INT );
Set thread priority
We can use the threadpriority attribute of the thread class to set the thread priority. The value range of thread priority is normal, abovenormal, belownormal, highest, or lowest. See the following configuration code:
Mythread. Priority = threadpriority. Highest;
Delay thread
The suspend method of the thread class can delay a thread (suspend a thread ). The thread is delayed until the resume method is called.
If (Mythread. Threadstate = threadstate. Running)
{
Mythread. Suspend ();
}
Restore delayed threads
Call the resume method to restore a delayed thread. If the thread is not delayed, the resume method is invalid.
If (Mythread. Threadstate = threadstate. Suspended)
{
Mythread. Resume ();
}