c# - Should/Could this "recursive Task" be expressed as a TaskContinuation? -
in application have need continually process piece(s) of work
on set interval(s). had written task
continually check given task.delay
see if completed, if work
processed corresponded task.delay
. draw method task
checks these task.delays
in psuedo-infinite loop when no task.delay
completed.
to solve problem found create "recursive task
" (i not sure jargon be) processes work @ given interval needed.
// new recurring work can added creating // task below , adding entry dictionary. // recurring work can removed/stopped looking // in dictionary , calling cts.cancel method. private readonly object _lockrecurwork = new object(); private dictionary<work, tuple<task, cancellationtokensource> recurringwork { get; set; } ... private task createrecurringworktask(work worktodo, cancellationtokensource tasktokensource) { return task.run(async () => { // work, wait prescribed amount of time before doing again dowork(worktodo); await task.delay(worktodo.recurrate, tasktokensource.token); // if work's cancellationtokensource not // cancelled "schedule" next work execution if (!tasktokensource.iscancellationrequested) { lock(_lockrecurwork) { recurringwork[worktodo] = new tuple<task, cancellationtokensource> (createrecurringworktask(worktodo, tasktokensource), tasktokensource); } } }, tasktokensource.token); }
should/could represented chain of task.continuewith
? would there benefit such implementation? there majorly wrong current implementation?
yes!
calling continuewith
tells task
call code finishes. far faster manually polling it.
Comments
Post a Comment