C # key Knowledge (vi)

Source: Internet
Author: User
Tags include sleep thread thread class
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

protected void HelloWorld ()
{
String str;
Console.Write ("HelloWorld");
}
}

Kill a Thread

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 ();
}




Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.