One, multi-threaded:
1. Concept:
thread: is the smallest unit of the Windows Task Scheduler. A thread is a flow of execution in a program, each with its own proprietary register (stack pointers, program counters, and so on), but the code area is shared, meaning that different threads can execute the same function. Multithreading: Refers to a program that contains multiple execution flows, that is, in one program can run several different threads to perform different tasks (code), that is, allow a single program to create multiple parallel execution of threads to complete their own tasks. 2, why to use multi-threaded:① Let the computer "at the same time" multiple things, save time. ② Multithreading allows a program to "simultaneously" handle multiple things. ③ Run the program in the background, improve the efficiency of the program, and will not make the main interface unresponsive situation. 3.4 steps to generate a thread:write the method that produces the thread to executereferencing the System.Threading namespaceinstantiate the thread class and pass in a delegate that points to the method that the thread is running. (This time the thread has been generated, but it is not yet running)call the Start method of the thread instance, marking that the thread can be executed by the CPU, but the specific execution time is determined by the CPU. 4.How to implement multithreading in. NET:threads must also be executing a piece of code. So to produce a thread, you must first write a method for the thread, which is the code that the thread runs to execute. (Find someone to do one thing)The method is called by a delegate when the thread starts. (Benefits of delegation)
(When a thread starts, a delegate is invoked, and the delegate executes the appropriate method to implement the thread execution method.)
Case code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespace_07 Multithreading {classProgram {Static voidMain (string[] args) {Console.WriteLine ("the main thread {0} is executing ...", Thread.CurrentThread.ManagedThreadId); #regionThread//ThreadStart a delegate without a parameter and no return valueThread thread =NewThread (SHOWMSG);//A new thread is opened to execute a method//It is recommended that the operating system make the current thread a high levelThread. Priority =threadpriority.highest; //for developers to identify different threadsThread. Name ="Zy"; //Background Thread: If all foreground threads are exited, the background thread is automatically closedThread. IsBackground =true; Thread. Start ();//does not execute, tells the operating system to be ready//thread. Abort ();//Close ThreadConsole.readkey (); #endregion } Static voidShowMsg () {Console.WriteLine ("worker thread {0} in execution ...", Thread.CurrentThread.ManagedThreadId); Thread.Sleep ( -); } }}View Code
Run:
5. Some important members of the thread class:
Start () startup threadabort () terminate threadThread.Sleep (1) static method that allows the current thread to stop running for a period of timeName thread nameThread.CurrentThread getting the current thread reference-----------------------------------------------------------------------------------Second, the delegate asynchronous invocation: 1, understand a few points:BeginInvoke Asynchronous InvocationEndInvoke Getting the (method) return value of an asynchronous callIAsyncResult The state of an asynchronous operationAsyncResult The result of an asynchronous operation (can get the currently executing delegate object)using System.Runtime.Remoting.Messaging;Note: The delegate's asynchronous call is only for unicast delegatesCase code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Runtime.Remoting.Messaging;namespace_07 Multithreading { Public Delegate intAdddel (intAintb);//declares a delegate with a parameter that has a return value classProgram {Static voidMain (string[] args) {Console.WriteLine ("the main thread {0} is executing ...", Thread.CurrentThread.ManagedThreadId); Adddel Adddel=NewAdddel (Addfunc); //1. Synchronous invocation//int c = Adddel (1, 2); //2. Asynchronous invocation with no callback function//starts the method that the delegate points to to execute, specifically by the thread pool, which provides the method to execute the current delegate point//IAsyncResult Ascresult = Adddel.begininvoke (1, 2, NULL, NULL); //While (!ascresult.iscompleted)//{ // //other operations performed by the main thread//} ////This EndInvoke method will block the current thread. Until the delegate method finishes executing,////And after giving the return value to result, proceed to the following code //int result = Adddel.endinvoke (Ascresult); //Console.WriteLine (result);//3//Console.readkey (); //3. Asynchronous invocation with callback functionIAsyncResult Ascresult = Adddel.begininvoke (1,2, Mydelcallback,3); //The main thread can continue to perform other operationsConsole.WriteLine ("==========="); Console.readkey (); } Static intAddfunc (intAintb) {Console.WriteLine ("addfunc worker threads running ... {0}", Thread.CurrentThread.ManagedThreadId); //Thread.Sleep (+); returnA +b; } //the asynchronous delegate executes the completed callback function Public Static voidmydelcallback (IAsyncResult result) {//converting an interface type to an instance typeAsyncResult Aresult =(AsyncResult) result; //convert to our own delegate typeAdddel del =(Adddel) aresult.asyncdelegate; //execution completion Gets the result of the execution intAddresult =del. EndInvoke (result); intState = (int) Aresult.asyncstate;//3Console.WriteLine ("The result of the asynchronous completion of the callback method execution is: {0} @{1}", Addresult, Thread.CurrentThread.ManagedThreadId); } } }View Code
Run:
To perform the steps:
① gets a thread from the line constructor.
② the method that executes the delegate point (executes within the worker thread)
③ calls the callback function (if NULL is not performed).
01-Multithreading and asynchronous delegates