What is EAP Asynchronous Programming mode
The EAP event-based Asynchronous pattern is proposed by. NET 2.0, and classes that implement the event-based Asynchronous Pattern will have one or more async-suffixed methods and corresponding completed events, and these classes support the cancellation of asynchronous methods, progress reports, and report results. However, not all classes in. NET support EAP, and the following 17 classes are summed up to support EAP async.
Derived types of System.Object:
System.Activies.WorkflowInvoke
System.Deployment.Application.ApplicationDeployment
System.Deployment.Application.InPlaceHosingManager
System.Net.Mail.SmtpClient
System.Net.PeerToPeer.PeerNameResolver
System.Net.PeerToPeer.Collaboration.ContactManager
System.Net.PeerToPeer.Collaboration.Peer
System.Net.PeerToPeer.Collaboration.PeerContact
System.Net.PeerToPeer.Collaboration.PeerNearMe
System.ServiceModel.Activities.WorkflowControlClient
System.ServiceModel.Discovery.AnnoucementClient
System.ServiceModel.Discovery.DiscoveryClient
Derived types of System.ComponentModel.Component:
System.ComponentModel.BackgroundWorker
System.Media.SoundPlay
System.Net.WebClient
System.Net.NetworkInformation.Ping
System.Windows.Forms.PictureBox (inherited from the control class, the control class is derived from the component class)
When the Xxxasync method of a class that is based on the event's EAP pattern is called, an asynchronous operation is initiated that notifies the thread pool of threads to perform time-consuming operations when the method call completes, so that when the UI thread calls the method, the UI thread is not blocked.
And the event-based EAP pattern is based on the APM pattern, and APM is built on the delegate. The following demo uses the BackgroundWorker class to demonstrate how to use EAP async.
Demo
The requirement to implement is also the example shown in the previous section APM, click the Request button, the UI thread returns immediately (the interface will not block), and then initiates the asynchronous request of the remote URL resource, when the asynchronous request completes, the content is displayed to the interface, the code is as follows.
1 // <summary>2 // Asynchronous EAP3 // </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 Private voidButton3_Click (Objectsender, EventArgs e)7{8BackgroundWorker worker =NewBackgroundWorker ();9Worker. DoWork + =NewDoworkeventhandler (DoWork);//Register DoWork events to implement asynchronousTenWorker. RunWorkerAsync ( This); One} A - // <summary> - /// Asynchronous Operation the // </summary> - /// <param name= "Sender" ></param> - /// <param name= "E" ></param> - Public voidDoWork (Objectsender, DoWorkEventArgs e) +{ - //Clear the previous query results first + This. richTextBox1.Text = ""; A atvar url = This. TextBox1.Text.Trim (); -var request = httpwebrequest.create (URL); -var response = Request. GetResponse (); -var stream = Response. GetResponseStream (); - using(StreamReader reader =NewStreamReader (Stream)) -{ invar content = reader. ReadToEnd (); - This. richTextBox1.Text = content; to} +}
View Code
Event-based Asynchronous mode (EAP)