Detailed as Java, writing a multithreaded application in C # is very simple, and this chapter describes how to develop multithreaded programs in C #. The. NET threads are defined by the System.Threading namespace. So you have to include this name space.
Using System.Threading;
Start a thread
The thread class in the System.Threading namespace describes a thread object, and you can create, delete, stop, and recover a thread by using the class object. Create a new thread to start the thread by using the new operation and through the start () method
thread = new Thread (new ThreadStart (HelloWorld));
Thread. Start ();
Note: Unlike a Java program, creating a new thread and invoking the start () method does not call the run () method, but rather passes the thread-calling program
Here is the function that initiates the thread execution
The Abort () method of the thread class can permanently kill a thread. You should determine whether a thread is alive during the lifetime before killing a thread.
if (thread. IsAlive)
{
Thread. Abort ();
}
Stop a thread
The Thread.Sleep method can stop a thread in a fixed cycle class
Thread. Sleep ();
Set Thread Priority
The ThreadPriority property in the thread class is used to set a threadpriority priority level. Thread priority levels include normal, AboveNormal, BelowNormal, highest, and lowest several.
Thread. Priority = Threadpriority.highest;
Suspend a thread
The suspend () method of the calling thread class suspends a thread until it is invoked using the resume () method. You should determine whether the thread is active during the activity before suspending a thread.
if (thread. ThreadState = threadstate.running)
{
Thread. Suspend ();
}
Evoke a thread
You can evoke a suspended thread by using the Resume () method. Before suspending a thread, you should determine if the thread is in the pending period, and if
The method does not work if the thread is not suspended.
if (thread. ThreadState = threadstate.suspended)
{
Thread. Resume ();
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.