C # multithreaded programming started

Source: Internet
Author: User

. NET defines the function of multithreading in the System.Threading namespace. Therefore, to use multiple threads, you must first declare a reference to this namespace (using System.Threading;).

Even if you do not have the experience of writing multithreaded applications, you may have heard the words "Start thread" "Kill thread", in addition to these two, there are also multithreading aspects such as "pause thread" "Priority" "Suspend thread" "Recovery thread" and so on. Below will be an explanation of one.

A. Start a thread

As the name suggests, "Start a thread" is to create a new and start a thread meaning, the following code can be implemented:

Thread thread1 = new Thread(new ThreadStart( Count));

The Count is the function that will be executed by the new thread.

B. Killing threads

"Kill a Thread" is to destroy the first thread, in order not to waste effort, before killing one of the threads, it is best to determine whether it is still alive (through the IsAlive property), and then can call the Abort method to kill the thread.

C. Pausing a thread

It means to keep a running thread dormant for a period of time. such as thread. Sleep (1000); is to keep the thread dormant for 1 seconds.

D. Priority

There's no need to explain this. The thread class has a ThreadPriority property that is used to set the priority, but does not guarantee that the operating system will accept the priority. The priority of a thread can be divided into 5 kinds: Normal, AboveNormal, BelowNormal, highest, Lowest. The specific implementation examples are as follows:

thread.Priority = ThreadPriority.Highest;

E. Suspending Threads

The suspend method of the thread class is used to suspend the thread, knowing that the resume is invoked before the thread can continue. If the thread is already suspended, it will not work.

if (thread.ThreadState = ThreadState.Running)
{
 thread.Suspend();
}

F. Recovery threads

Used to recover a thread that has been suspended so that it continues to execute, and it will 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.