APMのイベントベース非同期モード(EAP)-2
4319 ワード
EAPはWindowsフォームの開発に便利な非同期モードで、IDEで可視化できる設計と使用
.Netには多くのクラスがこのモードです
System.ComponentModel.Component-derived types
System.ComponentModel.BackgroundWorker
System.Media.SoundPlayer
System.Net.WebClient
System.Net.NetworkInformation.Ping
System.Windows.Forms.PictureBox (derived from Control)
System.Object-derived types
System.Net.Mail.SmtpClient
System.Deployment.Application.ApplicationDeployment
System.Deployment.Application.InPlaceHostingManager
System.Activities.WorkflowInvoker
System.ServiceModel.Activities.WorkflowControlClient
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.Discovery.AnnouncementClient
System.ServiceModel.Discovery.DiscoveryClient
TaskCompletionSourceを使用してEAPをTaskに変換
// The System.Net.WebClient class supports the Event-based Asynchronous Pattern WebClient wc = new WebClient(); // Create the TaskCompletionSource and its underlying Task object var tcs = new TaskCompletionSource(); // When a string completes downloading, the WebClient object raises the // DownloadStringCompleted event which will invoke our ProcessString method wc.DownloadStringCompleted += (sender, ea) => { // This code always executes on the GUI thread; set the Task’s state if (ea.Cancelled) tcs.SetCanceled(); else if (ea.Error != null) tcs.SetException(ea.Error); else tcs.SetResult(ea.Result); }; // Have the Task continue with this Task that shows the result in a message box // NOTE: The TaskContinuationOptions.ExecuteSynchronously flag is required to have this code // run on the GUI thread; without the flag, the code runs on a thread pool thread tcs.Task.ContinueWith(t => { try { System.Windows.Forms.MessageBox.Show(t.Result); } catch (AggregateException ae) { System.Windows.Forms.MessageBox.Show(ae.GetBaseException().Message); } }, TaskContinuationOptions.ExecuteSynchronously); // Start the asynchronous operation (this is like calling a BeginXxx method) wc.DownloadStringAsync(new Uri("http://Wintellect.com"));
APMのEAP APM比較、
メリット
欠点
EAP
Visual Stuidoと併用でき、設計時にEAPをサポートするのはSynchronizationContextでスレッドモデルを処理するため、GUIプログラムで使いやすい
APMよりも多くのメモリを消費し、より遅い速度EAPの異常は放出されません.
APM
APMは物事の本質に近いEAP内部はAPMで事前に
-
詳細:
Clr Via C#
http://transbot.blog.163.com
http://ys-f.ys168.com/?CLR_via_CSharp_3rd_Edition_Code_by_Jeffrey_Richter.zip_55bism1e0e7bkisjthit2bso0cm5bs4bs1b5bktnql0c0bu22f05f12z
// The System.Net.WebClient class supports the Event-based Asynchronous Pattern
WebClient wc = new WebClient();
// When a string completes downloading, the WebClient object raises the
// DownloadStringCompleted event which will invoke our ProcessString method
wc.DownloadStringCompleted += (s, e) =>
{
System.Windows.Forms.MessageBox.Show((e.Error != null) ? e.Error.Message : e.Result);
};
// Start the asynchronous operation (this is like calling a BeginXxx method)
wc.DownloadStringAsync(new Uri("http://Wintellect.com"));
.Netには多くのクラスがこのモードです
System.ComponentModel.Component-derived types
System.ComponentModel.BackgroundWorker
System.Media.SoundPlayer
System.Net.WebClient
System.Net.NetworkInformation.Ping
System.Windows.Forms.PictureBox (derived from Control)
System.Object-derived types
System.Net.Mail.SmtpClient
System.Deployment.Application.ApplicationDeployment
System.Deployment.Application.InPlaceHostingManager
System.Activities.WorkflowInvoker
System.ServiceModel.Activities.WorkflowControlClient
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.Discovery.AnnouncementClient
System.ServiceModel.Discovery.DiscoveryClient
TaskCompletionSourceを使用してEAPをTaskに変換
// The System.Net.WebClient class supports the Event-based Asynchronous Pattern WebClient wc = new WebClient(); // Create the TaskCompletionSource and its underlying Task object var tcs = new TaskCompletionSource
APMのEAP APM比較、
メリット
欠点
EAP
Visual Stuidoと併用でき、設計時にEAPをサポートするのはSynchronizationContextでスレッドモデルを処理するため、GUIプログラムで使いやすい
APMよりも多くのメモリを消費し、より遅い速度EAPの異常は放出されません.
APM
APMは物事の本質に近いEAP内部はAPMで事前に
-
詳細:
Clr Via C#
http://transbot.blog.163.com
http://ys-f.ys168.com/?CLR_via_CSharp_3rd_Edition_Code_by_Jeffrey_Richter.zip_55bism1e0e7bkisjthit2bso0cm5bs4bs1b5bktnql0c0bu22f05f12z