Write multithreaded applications in C #, simple!

zhaozj2021-02-16  117

???????? (Welcome to www.kunwsoft.com) ??????? When you use VB to implement multiple threads, you find 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 multithreaded 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 write multithreaded applications, you may have heard of "starting thread" "killing threads". In fact, in addition to these two, there are many threads. "Pause Thread" "Priority" "Suspending Thread" "Restore Thread", etc. Next, one will be explained below. ??????? a. Start the thread? ??? Where Count is a function that will be executed by the new thread. ??? b. Kill threads ??? "Kill thread" is the root of the first thread, in order not to make a good strength, it is best to judge whether it is still alive before killing a thread (through the isalive property), then you can Call the ABORT method to kill this thread. ??? c. Suspend the thread ??? It means that a running thread is sleeping 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 = threadpriority.highest; ??? E. 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??? is used to recover Hanging threads to let it continue, if the thread does not hang, it will not work. ??? if (thread.threadstate = threadstate.suspended) ???? {???????? thread.Resume (); ???} ??? Next, an example will be listed Thread tension function. This example comes from the help document.

??? using system; ??? using system.threading; ???? // simple threading scenario :? start a static method mething ??? 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 帖子 ????? ???? t threadproc method is caled when thread starts. ??????? // it looks ten time 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); ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Slice. ?????????????????????????????????????????????} ???????} ?????? 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 ??????????????????????????????????????????????????????????????????????????????????? ???? '// The thread.sleep That Follows T.Start () to see the difference. ??????????? T.Start (); ??????????? / /Thread.sleep(0); ??????????? for (int i = 0; i <4; i ) {??????????????. WriteLine ("Main Thread: Do some work."); ??????????????? thread.sleep (0); ???????????}??? ???????? console.writeline ("Main thread: Call Join (), to wait untric threadproc Ends."); ??????????? t.join ();??? ???????? console.writeline ("Main thread: threadproc.join haas returned."); ??????????? console.readline ();

???????} ???} ?????? The output generated by this code Similar to the following: Main Thread: Start A Second Thread.

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-8025.html

New Post(0)