The. NET Framework's system. Threading namespace provides classes and interfaces that can be programmed with multiple threads. In addition to synchronous thread activity and data access classes (mutex, monitor, interlocked, autoresetevent, etc.), this namespace also contains a threadpool class (which allows users to use the thread pool provided by the system) and a Timer class (it executes the callback method on the thread pool thread ).
The thread class is described as follows in msdn:
| Thread class [C #]:
Create and control a thread, set its priority, and obtain its status.
For a list of all members of this type, see Thread members.
System. Object
System. Threading. Thread
[Visual Basic]
Notinheritable public class thread
[C #]
Public sealed class thread
[C ++]
Public _ GC _ sealed class thread
[JScript]
Public class thread
Thread Security
This type is safe for multi-threaded operations.
Remarks
A process can create one or more threads to execute some program code associated with the process. Use threadstart to delegate the program code to be executed by the thread.
When a thread exists, it is always in one or more States defined by threadstate. The scheduling priority defined by threadpriority for a thread request, but the operating system cannot accept this priority.
Gethashcode identifies the managed thread. During the lifetime of a thread, no matter what the application domain obtains this value, it does not conflict with any value from other threads.
Note that the operating system threadid has no fixed relationship with the managed thread, because the unmanaged host can control the relationship between the managed thread and the managed thread. In particular, complex hosts can use the CLR hosting API to schedule many managed threads for the same operating system thread, or move managed threads between different operating system threads.
|
Let's look at the simplest example:
1. Create a new windows application and add three buttons on the form1 form.
2. Add the declaration: using system. Threading; to the start position of the form1.cs class;
3. Add the following to the form1 class:
Public void mythread1 ()
{// Thread 1
MessageBox. Show ("thread 1, you do not need to close the current dialog box to execute other functions of the program. "," System prompt ", messageboxbutton. OK, messageboxicon. information );
}
Public void mythread2 ()
{// Thread 2
MessageBox. Show ("thread 2, you can execute other functions of the program without closing the current dialog box. "," System prompt ", messageboxbutton. OK, messageboxicon. information );
}
4. The event code in a button is as follows:
Private void button#click (Object sender, system. eventargs E)
{// Thread 1
Thread mythread = new thread (New threadstart (mythread1 ));
Mythread. Start ();
}
Private void button2_click (Object sender, system. eventargs E)
{// Thread 2
Thread mythread = new thread (New threadstart (mythread2 ));
Mythread. Start ();
}
Private void button3_click (Object sender, system. eventargs E)
{// Normal Mode
MessageBox. Show ("other functions of the program can be executed only after the current dialog box is closed", "system prompt", messageboxbuttons. OK, messageboxicon. information );
}
// Note: the start () method changes the status of the current instance to threadstate. Running.