Write multithreaded applications in C #, simple!

xiaoxiao2021-03-06  168

.NET defines the multi-threaded functionality in the System.ThReading namespace. Therefore, to use multithreading, you must first declare this name space (

Using system.threading;).

Even if you don't have experience in multi-threaded applications, you may have heard of "starting thread" "killing threads", in fact, in addition to these two, there are many threads, such as "suspend thread" priority "Hanging Thread" "Restore Thread", etc. Next, one will be explained below.

a. Start thread

As the name suggests, "Start Thread" is new and started with a thread, the following code can be implemented: thread thread1 = New Thread (New ThreadStart (count)); where Count is a function that will be executed by the new thread.

b. Kill threads

"Kill thread" is to smash the first thread, in order not to make a good gas, it is best to judge whether it is still alive before killing a thread, and then call the Abort method to kill this thread. C. Term thread

It means to let a running thread sleep for a while. Such as thread.sleep (1000); is to let the thread sleep for 1 second. D. Priority

This is not explained. There is a ThreadPriority property in the thread class that sets priority, but cannot guarantee that the operating system accepts this priority. A thread has a priority of 5: Normal, Abovenormal, Belownormal, Highest, Lowest. Specific implementations are as follows: thread.priority = threadpriority.Highest;

e. hang thread

The SUSPEND method of the Thread class is used to suspend the thread, know that the resume can continue to be executed. If the thread has hang, it will not work. IF (thread.threadstate = threadstate.running)? {Thread.suspend ();}

f. recovery thread

It is used to restore the already pending thread to let it continue, and if the thread does not hang, it will not work. IF (thread.threadstate = threadState.suspended)? {Thread.resume ();} An example will be listed to illustrate simple thread tension. This example comes from the help document.

using System; using System.Threading;. // Simple threading scenario :? Start a static method running // on a second thread public class ThreadExample {// The ThreadProc method is called when the thread starts // It loops ten times,. Writing to the constel and yielding? // the rest of its Time slice each time, and then ends. public static void threadproc () {for (int i = 0; i <10; i ) {Console.Writeline ("ThreadProc: {0} ", i); // yield The rest of the time slice. Thread.sleep (0);}} public static void main () {Console.writeline (" Main Thread: Start a second thread. "); // The constructor for the Thread class requires a ThreadStart? // delegate that represents the method to be executed on the? // thread.? C # simplifies the creation of this delegate. Thread t = new Thread (new ThreadStart (ThreadProc)) ; // Start ThreadProc.? On a uniprocessor, the thread does not get? // any processor time until the main thread yields.? Uncomment? // the Thread.Sleep that follows t.Start () to see the difference. T.Start ( ); //Thread.sleep (0); for (int i = 0; i <4; i ) {console.writeline ("Main thread: do some work."); Thread.sleep (0);} console. WriteLine ("Main Thread: Call Join (), To Wait Until ThreadProc Ends."); T.JOIN (); console.writeline ("Main thread: threadproc.join haas returned.? Press Enter to End Program); Console.readline ();

}} The output generated by this code is similar to the following: main thread: start a second a second a second.

Main trread: do some work.

ThreadProc: 0

Main trread: do some work.

ThreadProc: 1

Main trread: do some work.

ThreadProc: 2

Main trread: do some work.

ThreadProc: 3

Main thread: Call Join (), To Wait Until ThreadProc Ends.

ThreadProc: 4

Threadproc: 5

ThreadProc: 6

ThreadProc: 7

ThreadProc: 8

ThreadProc: 9

Main thread: threadproc.join haas returned. Press Enter to End Program.?

转载请注明原文地址:https://www.9cbs.com/read-128133.html

New Post(0)