c# - WebClient progress reporting using TAP -


i'm wondering if there way report webclient progress without using eap(event-based asynchronous pattern). old way(using eap) be:

var client = new webclient(); client.downloadprogresschanged += (s,e) => { //progress reporting } client.downloadfilecompleted += (s,e) => { console.write("download finished" } client.downloadfileasync(file); 

with async/await can written as:

var client = new webclient(); client.downloadprogresschanged += (s,e) => { //progress reporting } await client.downloadfiletaskasync(file); console.write("downlaod finished"); 

but in second example i'm using both eap , tap(task-based asynchronous pattern). isn't mixing 2 patterns of asynchrony considered bad practice?

is there way achieve same without using eap? have read iprogress interface, think there no way use report webclient progress.

the bad news answer no!

the news eap api can converted tap api.

try this:

public static class webclientextensios {     public static async task downloadfiletaskasync(         webclient webclient,          uri address,          string filename,          iprogress<tuple<long, int, long>> progress)     {         // create task returned         var tcs = new taskcompletionsource<object>(address);          // setup callback event handler handlers         asynccompletedeventhandler completedhandler = (cs, ce) =>         {             if (ce.userstate == tcs)             {                 if (ce.error != null) tcs.trysetexception(ce.error);                 else if (ce.cancelled) tcs.trysetcanceled();                 else tcs.trysetresult(null);             }         };          downloadprogresschangedeventhandler progresschangedhandler = (ps, pe) =>         {             if (pe.userstate == tcs)             {                 progress.report(                     tuple.create(                         pe.bytesreceived,                          pe.progresspercentage,                          pe.totalbytestoreceive));             }         };          try         {             webclient.downloadfilecompleted += completedhandler;             webclient.downloadprogresschanged += progresschangedhandler;              webclient.downloadfileasync(address, filename, tcs);              await tcs.task;         }                 {             webclient.downloadfilecompleted -= completedhandler;             webclient.downloadprogresschanged -= progresschangedhandler;         }     } } 

and use this:

void main() {     var webclient = new webclient();      webclient.downloadfiletaskasync(         new uri("http://feeds.paulomorgado.net/paulomorgado/blogs/en"),         @"c:\temp\feed.xml",         new progress<tuple<long, int, long>>(t =>         {             console.writeline($@"         bytes received: {t.item1,25:#,###}    progress percentage: {t.item2,25:#,###} total bytes receive: {t.item3,25:#,###}");         })).wait(); } 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -