C # implement asynchronous programming,
Recently, asynchronous programming has been used in several. Net projects. As a. Net nickname, it is necessary to study C # asynchronous programming well.
What is asynchronous
Asynchronous means to wait for the task to complete without blocking the current thread. Instead, it throws the task to the thread pool for execution. The current thread can continue to run down until other threads finish the task, and callback to notify the current thread. The entire task is completed asynchronously from start to end and does not block the current thread. Therefore, asynchronous is very important,The current thread is not blocked..
Implement Asynchronous Programming
In the C # language, asynchronous programming is implemented through delegation. Two Methods BeginInvoke () and EndInvoke () are defined in the delegate type ().
/// <Summary> /// start the asynchronous operation. /// </summary> /// <param name = "param"> parameters of the delegate method </param> /// <param name = "callback"> </param >/// <param name = "object"> </param> /// <returns> </returns> IAsyncResult BeginInvoke (int param, asyncCallback callback, Object @ object );
/// <Summary> /// end the asynchronous operation and return the asynchronous operation result. /// </summary> /// <param name = "result"> </param> /// <returns> return type of the delegate method </returns> string EndInvoke (IAsyncResult result );
The following code is used to implement asynchronous programming.
1 /// <summary> 2 /// define a delegate Type 3 /// </summary> 4 public delegate string Del (); 5 6 /// <summary> 7 // task class 8 /// </summary> 9 public class TaskClass10 {11 public static string SleepTask () 12 {13 Console. writeLine ("asynchronous thread (ThreadId = {0}) starts to run the sleep task, sleep for 3 s. \ n ", Thread. currentThread. managedThreadId); 14 15 threads. sleep (3000); 16 17 Console. writeLine ("asynchronous thread (ThreadId = {0}) Wake up, sleep task execution ends. \ n ", Thread. currentThread. managedThreadId); 18 19 return "the asynchronous thread is successfully executed. \ n "; 20} 21} 22 23 // <summary> 24 // asynchronous programming implementation class (the main thread is not blocked) 25 /// </summary> 26 public class AsynCallbackClass27 {28 public static string result; 29 30 public static void Main (string [] args) 31 {32 Console. writeLine ("main thread (ThreadId = {0}) starts execution. \ n ", Thread. currentThread. managedThreadId); 33 34 Del del = new Del (TaskClass. sleepTask); 35 36 Console. writeLine ("main thread (ThreadId = {0}) calls the BeginInvoke () method to execute asynchronous tasks. \ n ", Thread. currentThread. managedThreadId); 37 38 // start to execute asynchronous operations, TaskClass. sleepTask has no parameters. Callback is the defined Callback method. 39 del. beginInvoke (Callback, null); 40 41 Console. writeLine ("main thread (ThreadId = {0}) continues execution. \ n ", Thread. currentThread. managedThreadId); 42 43 Thread. sleep (5000); 44 45 // outputs the result 46 Console returned by the asynchronous task. writeLine (result); 47 48 Console. writeLine ("main thread (ThreadId = {0}) Execution ended. \ n ", Thread. currentThread. managedThreadId ); 49} 50 51 // <summary> 52 // callback method 53 // </summary> 54 // <param name = "ar"> </param> 55 public static void Callback (IAsyncResult ar) 56 {57 AsyncResult asyncResult = ar as AsyncResult; 58 59 if (asyncResult = null) {return;} 60 61 Del del = asyncResult. asyncDelegate as Del; 62 63 if (del = null) {return;} 64 65 Console. call the EndInvoke () method in the WriteLine ("Callback method to obtain the asynchronous task result. \ n ", Thread. currentThread. managedThreadId); 66 67 // end the asynchronous operation and return the asynchronous task result. 68 result = del. endInvoke (ar); 69} 70}
Running result:
From the running results, we can find that the main thread continues to execute the BeginInvoke () method without blocking, and the task is indeed executed by a new thread, after the task is executed, call the callback method and call the EndInvoke () method in the callback method to obtain the task execution result.
Summary
1. In asynchronous programming, the current thread will not be blocked.
2. the C # delegation mechanism can easily implement asynchronous programming.