Task.Start vs. Async


does know advantages/disadvantes of each:

task

async/await

they pretty similar.


jp cowboy coders unite!

the new async/await features build on top of task , task<t>.  they make life far easier work with, code doesn't have turned inside out or have strange language work.

you can of same things them, though - example, wanted connect async service (that returned task<int>) , update text box, task in .net 4:

// ie: in button event handler - private void button1_click(object sender, eventargs e) {     task<int> task = someservice.getdataasync();      task.continuewith(t =>     {       try       {         int data = t.result;         textbox1.text = string.format("received {0}", data);       }       catch(aggregateexception ae)       {           // aggregateexception, custom handling required           ae.handle( ex =>           {               if (ex mycustomexception)               {                   textbox1.text = "received mycustomexception";               }                logexception(ex); // log this/handle somehow/etc               return true; // mark handled           });       }     }, taskscheduler.fromcurrentsynchronizationcontext()); }

this same code, using async/await, gets far simpler:

// ie: in button event handler - private async void button1_click(object sender, eventargs e) {     try     {         int data = await someservice.getdataasync();         textbox1.text = string.format("received {0}",      }     catch(mycustomexception ce) // catch "proper" exception type directly     {          textbox1.text = "received mycustomexception";     }     catch (exception e)     {          logexception(e);     } }

these behave same way - second far simpler use , work with...


reed copsey, jr. - http://reedcopsey.com
if post answers question, please click "mark answer" on post , "mark helpful".



Archived Forums V  >  Visual Studio Async CTP



Comments

Popular posts from this blog

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

Azure DocumentDB Owner resource does not exist

SQL Server 2008 - High Memory Usage