In the previous blog post documenting the knowledge of C # APM asynchronous programming, share the knowledge of asynchronous programming with the EAP (asynchronous event-based programming pattern) today. The following will continue to be on the TPL task Parallel library knowledge, like friends Please continue to pay attention to OH.
EAP asynchronous programming is a complement to APM in C #, allowing asynchronous programming to have a series of state events. If you have read the previous article in this series, "C # Asynchronous Programming 1 APM Asynchronous Program development" and assume that you are a member of the Microsoft C # language Development group, you are now designing an event-based asynchronous programming pattern. So you're going to use the previous APM to transform it? Or is it a re-creation? So when you decompile the associated DLL, it's a surprise to find that EAP is actually a package that adds event events to APM. So this blog post is not very long, we only introduce the basic information and programming implementation of EAP mode. For event Events I add a post to illustrate its function and usage.
EAP Event-based asynchronous programming pattern
When we use EAP mode for asynchronous programming, we need to meet the following 2 conditions:
1. The method name to be asynchronous should end with Xxxasync
2, to have an event called xxxcompleted to listen to the completion of the asynchronous method
3. You can add an CancelAsync method to cancel an asynchronous method that is executing (optional)
Example code:
/// <summary> ///EAP is an encapsulation of APM/// </summary> Public classWorker { Public enumWorkerstatus {Cancel=0, running=1, completed=2 } Public classWorkereventargs:eventargs { PublicWorkerstatus Status {Get;Set; } Public stringMessage {Get;Set; } } PublicWorker () {} Public EventEventhandler<workereventargs>onworkcompleted; IAsyncResult AsyncResult=NULL; Thread Thread=NULL; Public voidWorkasync () {Worker _this= This; Action Action= () + ={thread=Thread.CurrentThread; Thread.Sleep (10000); Console.WriteLine (string. Format ("thread: {0},work over.", Thread.CurrentThread.ManagedThreadId)); }; AsyncResult= action. BeginInvoke (Result) ={Workereventargs e=NULL; Try{action.} EndInvoke (AsyncResult); } Catch(ThreadAbortException ex) {e=NewWorkereventargs () {Status = Workerstatus.cancel, Message ="The asynchronous operation was canceled" }; } if(NULL!=_this. onworkcompleted) {_this. Onworkcompleted.invoke ( This, E); } }, This); } Public voidCancelAsync () {if(NULL!=thread) thread. Abort (); } }
The calling program uses WinForm to do this, and the sample code is as follows (for demonstration purposes only):
worker worker; Private voidBtn_start_click (Objectsender, EventArgs e) {Worker=NewWorker (); Worker. Onworkcompleted+=workover; Worker. Workasync (); Console.WriteLine (string. Format ("thread: {0}", Thread.CurrentThread.ManagedThreadId)); } Private voidBtn_cancel_click (Objectsender, EventArgs e) {Worker.} CancelAsync (); } Private voidWorkover (Objectsender, Worker.workereventargs e) { if(NULL!=e) {if(Worker.WorkerStatus.Cancel = =e.status) {MessageBox.Show (e.message); } } Else{Console.WriteLine (string. Format ("thread: {0}, the delegate callback is complete.", Thread.CurrentThread.ManagedThreadId)); } }
Effect Show:
Precautions (important):
1, APM asynchronous programming, because asynchronous code execution in a separate thread, the exception that occurs in asynchronous code should be caught when calling EndXxx.
2, EAP asynchronous programming, for the same reason, the exception information in the code will be passed to the EventArgs parameter of the completed event.
EAP (Event-based Asynchronous programming model) is not much core content, and such as network, IO and other time-consuming class Ms has added the EAP-related asynchronous functions, we directly use it. In particular, the subsequent TPL parallel task, the user to customize the EAP asynchronous function of the opportunity is more, but the EAP in the relevant knowledge points such as threads, delegates, events and so on is still important ....
The TPL Task Parallel Library is rushing to the middle of the ....
C # Asynchronous Programming 2 EAP Asynchronous program development