From: www.kunwsoft.com
It was previously used to realize multi-threads before using VB, and found a certain difficulty. Although there is also such a method, but you don't have any satisfaction, but in C #, you must write multithreaded applications. This article will make a brief introduction to the role of throwing bricks!
.NET defines the multi-threaded functionality in the System.ThReading namespace. Therefore, to use multithreading, you must first declare this name space (use 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, "Start Thread" is new and started with a thread, and the following code can be implemented:
Thread thread1 = new thread (new threadstart (count);
The 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. The THREAD class is fortunate to HREADPRIORITY properties, it is used to set priority, but it cannot be guaranteed
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.suspend)
{
Thread.Resume ();
}
An example will be listed below to illustrate a 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 console 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, t He Thread Does NOT GET // Any Processor Time Until The Main Thread Yield. 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 Has Returned. Press Enter to End Program); console.readline ();
}} The output produced by this code is similar to the following: Main Thread: Start A Second Thread. Main Thread: Do Some Work. Threadproc: 0 Main Thread: Do Some Work. Threadproc: 1 Main Thread: Do some Work. Threadproc: 2 Main thread: 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 has returned. Press Enter to End Program.