From: www.kunwsoft.com found a certain difficulty when using VB to implement multiple threads. 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. Starting the thread as the name, "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 thread "kill thread" is to smash the first-threaded ride, in order not to do good, it is best to determine if it is still alive before killing a thread (through the isalive property), then call the Abort method to kill This thread is. C. Suspension threads 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. The specific implementation examples are as follows: thread.priority = threadprioriity.Highest; E. Suspend method for suspend the thread THREAD class is used to suspend the thread, know that the RESUME can continue. If the thread has hang, it will not work. If (thread.threadstate = threadstate.running) {thread.suspend ();} f. Recovery thread is used to restore the already pending thread so that it will continue, and if the thread does not hang, it will not work. IF (thread.threadstate = threadState.suspended) {thread.resume ();} An example will be listed below 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 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, 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 Has Returned. Press Enter to End Program);
Console.readline ();}} The output generated by this code 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 Transfection: Dynamic Network Production Guide www.knowsky.com