C # Multithreading

Source: Internet
Author: User

The multithreading function in c # is defined in the System. Threading namespace. Therefore, to use multithreading, you must first declare and reference this namespace (using System. Threading ;).

Start thread
As the name implies, "starting a thread" means creating and starting a thread. The following code can be implemented:
Thread thread1 = new Thread (new ThreadStart (Count ));
The Count is the method to be executed by the new thread.

Pause a 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.

Delete thread

Before deleting a thread, you 'd better determine whether it still exists (through the IsAlive attribute) and then call the Abort method to delete the thread.

Priority

There is no need to explain this. The hreadPRiority attribute in the Thread class, 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;

Suspend thread

The Suspend method of the Thread class is used to Suspend a Thread until Resume is called. If the thread has been suspended, it will not work.
If (thread. ThreadState = ThreadState. Running)
{
Thread. Suspend ();
}

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

 

Related Article

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.