Should/Could this “recursive Task” be expressed as a TaskContinuation?

断了今生、忘了曾经 提交于 2019-12-24 05:54:07

问题


In my application I have the need to continually process some piece(s) of Work on some set interval(s). I had originally written a Task to continually check a given Task.Delay to see if it was completed, if so the Work would be processed that corresponded to that Task.Delay. The draw back to this method is the Task that checks these Task.Delays would be in a psuedo-infinite loop when no Task.Delay is completed.

To solve this problem I found that I could create a "recursive Task" (I am not sure what the jargon for this would be) that processes the work at the given interval as needed.

// New Recurring Work can be added by simply creating 
// the Task below and adding an entry into this Dictionary.
// Recurring Work can be removed/stopped by looking 
// it up in this Dictionary and calling its 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 () =>
    {
        // Do the Work, then wait the prescribed amount of time before doing it again
        DoWork(workToDo);
        await Task.Delay(workToDo.RecurRate, taskTokenSource.Token);

        // If this Work's CancellationTokenSource is not
        // cancelled then "schedule" the next Work execution
        if (!taskTokenSource.IsCancellationRequested)
        {
            lock(_LockRecurWork)
            {
                RecurringWork[workToDo] = new Tuple<Task, CancellationTokenSource>
                    (CreateRecurringWorkTask(workToDo, taskTokenSource), taskTokenSource);
            }
        }
    }, taskTokenSource.Token);
}

Should/Could this be represented with a chain of Task.ContinueWith? Would there be any benefit to such an implementation? Is there anything majorly wrong with the current implementation?


回答1:


Yes!

Calling ContinueWith tells the Task to call your code as soon as it finishes. This is far faster than manually polling it.



来源:https://stackoverflow.com/questions/39457595/should-could-this-recursive-task-be-expressed-as-a-taskcontinuation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!