本文介绍了多任务延续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明白这个场景更清晰一点:

I would like to understand this scenario a little clearer:

考虑以下code:

frmProgressAsync prog = new frmProgressAsync(true);
TaskWithProgress t = new TaskWithProgress("Smoothing CP", true);
t.Task = A.ShowMovingAverage(tension,t.Progress)
       .ContinueWith(prev =>
           {
               t.ProgressInformation.Report("Smoothing FG");
               B.ShowMovingAverage(tension, t.Progress);
           });
await prog.RunAsync(t);

我有两个任务,我希望运行 A.ShowMovingAverage B.ShowMovingAverage 。都返回任务。

prog.RunAsync()的方法,我有以下几点:

In the prog.RunAsync() method, I have the following:

    public virtual Task RunAsync(TaskWithProgress task)
    {
        Show();
        TaskIsRunning();
        task.ContinueWith(Close(), CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, this._Scheduler);
        return task.Task;
    }

所以,有效的,我有三个任务,又一个previous后运行就完成了。

So effectively I have three tasks, to run in turn one after the previous is complete.

现在我的问题是在一些情况下,第一项任务几乎立即完成。当调用 prog.RunAsync()制成,并最终延续附加到任务时,它会立即运行,关闭窗体。

Now my issue is in some cases the first task completes virtually immediately. When the call to prog.RunAsync() is made, and the final continuation is appended to the task, it immediately runs, closing the form.

我可以看到,如果我在最后 ContinueWith()通话打破,任务状态为 RanToCompletion ,但我有点期待延续重置此回正在进行中。

I can see if I break on the last ContinueWith() call, the task status is RanToCompletion, but I am kind of expecting the continuation to reset this back to in progress.

可能有人请稍微更干净解释这种现象?并提供了一​​个潜在的解决方案,使最终延续之前的所有任务(延续)完成?

Could someone please explain this behavior a little more cleanly? And provide a potential solution so that all the tasks (continuations) complete before the final continuation?

推荐答案

任务唯一完整的一次。当您将延续,正在创建的先行任务完成时启动一个新的任务。所以,我相信你所看到的问题是, RunAsync 被安装在关闭延续,但不与做任何事情后续任务;考虑 task.Task = task.ContinueWith(关闭(),...)

Tasks only complete one time. When you attach a continuation, you are creating a new task that starts when the antecedent task completes. So, I believe the problem you're seeing is that RunAsync is attaching the Close continuation but not doing anything with the continuation task; consider task.Task = task.ContinueWith(Close(), ...).

不过,我推荐你使用等待而不是 ContinueWith 的。我发现,通常澄清了code,使得它更容易理解。

However, I recommend that you use await instead of ContinueWith. I find that usually clarifies the code and makes it easier to understand.

frmProgressAsync prog = new frmProgressAsync(true);
TaskWithProgress t = new TaskWithProgress("Smoothing CP", true);
t.Task = ShowMovingAveragesAsync(A, B, tension, t.Progress);
await prog.RunAsync(t);

private async Task ShowMovingAveragesAsync(TA A, TA B, TT tession, IProgress<string> progress)
{
  progress.Report("Smoothing FG");
  await A.ShowMovingAverageAsync(tension, progress);
  progress.Report("Smoothing FG");
  await B.ShowMovingAverageAsync(tension, progress);
}

public virtual async Task RunAsync(TaskWithProgress task)
{
  Show();
  TaskIsRunning();
  await task;
  Close();
}

这篇关于多任务延续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 02:32