c# - How to ensure just one instance of a Task is running? -


i have "long running function" needs triggered on timer, , able triggered button click. while task running, button switch 'cancel' button. want single instance of task running @ time. i've made task private variable timer can check iscompleted status of task. created private variable

private task sync; 

i have function task

private async task syncusers() {     // } 

right now, on button click, have variable assigned function

sync = syncusers(); await sync; 

it seems when task completed, that's it. how restart task/function? tried using task factory , async delegate , did not work.

sync = task.factory.startnew(async delegate { await syncstudents(); }); await sync; 

i have feeling i'm going wrong not sure start.

private task sync; private readonly object synclock = new object(); private task syncusers() {     lock (synclock)     {         if (this.sync == null || this.sync.iscompleted)         {             this.sync = ...         }     }      return this.sync; } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -