C# 5.0 async void vs async Task

. 1   Private  void the button1_Click ( Object SENDER, EventArgs E)
 2          {
 . 3              TestReturnsVoid (); // throws an exception in UI Thread, program crash 
. 4          }
 . 5  
. 6          Private  void the button2_Click ( Object SENDER, EventArgs E)
 . 7          {
 . 8              TestReturnsTask ( ); // will effect a further thread exception is thrown, the similar TheadPool 
. 9          }
 10  
. 11          Private  the async  void TestReturnsVoid ()
 12 is          {
 13 is              the awaitThe Test ();
 14          }
 15  
16          Private  the async the Task TestReturnsTask ()
 . 17          {
 18 is              the await the Test ();
 . 19          }
 20 is  
21 is          Private  the async the Task the Test ()
 22 is          {
 23 is              the throw  new new the NotImplementedException ();
 24          } 

Conclusion
1. async void only recommend event handler is used in the UI
2. always handle events: TaskScheduler.UnobservedTaskException

 

Guess you like

Origin www.cnblogs.com/0xMe/p/11982974.html