C # asynchronous programming 2 EAP asynchronous program development,
I recorded the knowledge of C # APM asynchronous programming in the previous blog. Today I will share with you the knowledge of EAP (Event-based asynchronous programming mode) asynchronous programming. We will continue to provide the knowledge of the TPL task parallel library later. If you like it, please stay tuned.
EAP asynchronous programming is a supplement of C # To APM, so that asynchronous programming has a series of State events. If you have read the previous article C # asynchronous programming 1 APM asynchronous program development in this series and assume that you are a member of the Microsoft C # Language Development Team, now let's design an event-based asynchronous programming mode. So you will use the previous APM for transformation? Or create another one? So when you decompile the related dll, you will be pleasantly surprised to find that EAP is actually adding the event encapsulation to APM. Therefore, this blog article will not be very long. We will only introduce the basic information and programming implementation of the EAP mode. For event Events, I will add a blog post to describe their functions and usage.
Event-based Asynchronous EAP programming mode
When we use the EAP mode for asynchronous programming, we need to meet the following two conditions:
1. The method name of the method to be asynchronous should end with XXXAsync
2. There must be an event named XXXCompleted to listen to the completion of the Asynchronous Method.
3. You can add a CancelAsync method to cancel the asynchronous method being executed (optional)
Sample Code:
/// <Summary> // EAP is the encapsulation of APM // </summary> public class Worker {public enum WorkerStatus {Cancel = 0, Running = 1, completed = 2} public class WorkerEventArgs: EventArgs {public WorkerStatus {get; set;} public string Message {get; set ;}} public Worker () {} public event EventHandler <WorkerEventArgs> OnWorkCompleted; IAsyncResult asyncResult = null; Thread thread = null; public void Wor KAsync () {Worker _ this = this; Action action = () => {thread = Thread. currentThread; Thread. sleep (1, 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 = new WorkerEventArgs () {Status = WorkerStatus. Cancel, Message = "Asynchronous Operation canceled"};} if (null! = _ This. OnWorkCompleted) {_ this. OnWorkCompleted. Invoke (this, e) ;}, this) ;}public void CancelAsync () {if (null! = Thread) thread. Abort ();}}
The calling program uses WinForm. The sample code is as follows (for demonstration only ):
Worker worker; private void Btn_Start_Click (object sender, EventArgs e) {worker = new Worker (); worker. onWorkCompleted + = WorkOver; worker. workAsync (); Console. writeLine (string. format ("Thread: {0}", Thread. currentThread. managedThreadId);} private void Btn_Cancel_Click (object sender, EventArgs e) {worker. cancelAsync ();} private void WorkOver (object sender, 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 completed. ", Thread. currentThread. managedThreadId ));}}
Effect display:
Note (important ):
1. APM asynchronous programming, because asynchronous code is executed in a separate thread, exceptions in asynchronous code should be captured when EndXXX is called.
2. In EAP asynchronous programming, the exception information in the code will be passed to the EventArgs parameter of the Completed event for the same reason.
EAP (Event-based asynchronous programming mode) core content is not much, and time-consuming MS such as network, IO has added EAP-related asynchronous functions, we can directly use it. In particular, the addition of subsequent TPL parallel tasks gives users the opportunity to customize the EAP asynchronous function, however, the knowledge related to EAP, such as threads, delegates, and events, is still important .......................
The Parallel library of the TPL task is approaching ...............