c# - Using async void to implement dataprovider -


i have interface this

public interface iserverdataprovider  {     string val1 { get; }     string val2 { get; }      event eventhandler<eventargs> val1changed;     event eventhandler<eventargs> val2changed; } 

it gives user access 2 strings retrieved server , events triggered when these strings change.

learning async-await in c#, can make simple implementation periodically checks if these values changed on server :

public class serverdataproviderasync : iserverdataprovider {     public event eventhandler<eventargs> val1changed;     public event eventhandler<eventargs> val2changed;      private string _val1url = "someurl";     private string _val2url = "otherurl";     private const int _delayms = 1000;      public serverdataproviderasync()     {         start();     }      private async void start()     {         val1 = await downloadstring(_val1url);         val2 = await downloadstring(_val2url);          val1updateloop();         val2updateloop();     }      private async void val1updateloop()     {         await task.delay(_delayms);         val1 = await downloadstring(_val2url);         val1updateloop();     }      private async void val2updateloop()     {         await task.delay(_delayms);         val2 = await downloadstring(_val1url);         val2updateloop();     }      private string _val1;     public string val1     {         { return _val1; }         private set         {             if (_val1 != value && value != null)             {                 _val1 = value;                 oncontentchanged(val1changed);             }         }     }      private string _val2;     public string val2     {         //similar val1     }      private async task<string> downloadstring(string url)     {         using (var wb = new webclient())         {             try { return await wb.downloadstringtaskasync(url); }             catch { /*log error*/}         }         return null;     }      private void oncontentchanged(eventhandler<eventargs> handler)     {         if (handler != null)         {             handler(this, eventargs.empty);         }     } } 

and can used mainwindow :

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();         var dataprovider = new serverdataproviderasync();         //hook events , display strings in gui     } } 

now question if implementaion? there better way?

the first part i'm worried async void methods. i've read should used event handlers. bad in case? , if so, why?

the other thing i'm worried recursive way update loops work. seems since awaits tasks not finished, not keep adding call stack.

you should use [iterative] infinite loop create infinite loop rather using infinite recursion.

using recursion means spending effort re-create exact same state machine scratch each iteration instead of using fine state machine have, , needlessly obfuscates code , reduces clarity (to point weren't sure of possible negative repercussions; don't want every single other person reads code have think through same problem) no real gain. additionally, if want able propagate exceptions generated in method caller (discussed further below) using recursion has number of problems, such messing call stack, making throwing exception through of levels difficult, , creating memory leak in each "finished" state machine wouldn't able cleaned up.

as methods being void, that's not particularly problematic. reason 1 want task returned can tell when operation finishes. operations never finish, run forever. getting task never completed isn't more or less useful not getting task @ in circumstances.

the way might relevant error handling. if loop generates error , method void needs responsible handling error itself, because conceptually top level method. if returns task gets luxury of throwing exception caller , leaving caller responsible handling exception. reason not return void method supposed run forever.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -