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
Post a Comment