Async'ing Your Way to a essful App with :你的成功async'.pptx
Async void is only for top-level event handlers. Use the threadpool for CPU-bound code, but not IO-bound. Use pletionSource to wrap Tasks around events. Libraries shouldn't lie, and should be chunky. Key Takeaways Async void is only for event handlers User: “ It mostly works, but not 100% reliably. ” Diagnosis & Fix: Probably was using async void. Should return Task not void. For goodness ’ sake, stop using async void! Async void is only for event handlers private async void Button1_Click( object Sender, EventArgs e) { try { SendData ( "https://secure./services/ oauth / request_token " ); await Task .Delay (2000 ); DebugPrint ( "Received Data: " + m_GetResponse ); } catch ( Exception ex) { ( "Error posting data to server." + ); } } private async void SendData( string Url) { var request = WebRequest .Create ( Url ); using ( var response = await ()) using ( var stream = new StreamReader ( ())) m_GetResponse = (); } Async void is only for event handlers private async void Button1_Click( object Sender, EventArgs e ) { try { SendData ( "https://secure./services/ oauth / request_token " ); // await (2000); // DebugPrint ("Received Data: " + m_GetResponse ); } catch ( Exception ex) { ( "Error posting data to server." + ); } } private async void SendData( string Url) { var request = WebRequest .Create ( Url ); using ( var response = await ()) // exception on resumption using ( var stream = new StreamReader ( ())) m_GetResponse = (); } Async void is only for event handlers Principles Async void is a “ fire-and-forget ” mechanism... The caller is unable to know when an async void has finished The caller is unable to catch exceptions thrown from an async void (instead they get p
Async'ing Your Way to a essful App with :你的成功async' 来自淘豆网www.taodocs.com转载请标明出处.