【问题标题】:Am I using ContinueWith correctly?我正确使用 ContinueWith 吗?
【发布时间】:2016-03-12 16:07:44
【问题描述】:

我有一个带有启动窗口(在 WPF 中)的应用程序,它允许用户登录、选择数据库、进行一些其他设置,并最终(成功后)显示一个进度条。进度完成后,将显示一个主窗口(在 WinForms 中)。

为了使进度条更新顺畅,我尽可能地远离 UI 线程。这是当前的方法:

var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
var backgroundScheduler = TaskScheduler.Default;

var connectingTask = Task.Factory.StartNew(() =>
{
    System.Diagnostics.Debug.WriteLine("connecting started");

    // some code that connects to a server; may fail

    System.Diagnostics.Debug.WriteLine("connecting finishing");
}

var connectFailAction = new Action<Task>((t) =>
{
    System.Diagnostics.Debug.WriteLine("connectFail started");

    // let user know that connecting failed, then exit

    System.Diagnostics.Debug.WriteLine("connectFail finishing");
});

var postConnectAction = new Action<Task>((t) =>
{
    System.Diagnostics.Debug.WriteLine("postConnect started");

    // some initial post-connection setup code; runs in UI thread

    System.Diagnostics.Debug.WriteLine("postConnect finishing");
});

var dbLoadingAction = new Action<Task>((t) =>
{
    System.Diagnostics.Debug.WriteLine("dbLoading started");

    // code that loads settings from the database; runs in background

    System.Diagnostics.Debug.WriteLine("dbLoading finishing");
});

var runGuiAction = new Action<Task>((t) =>
{
    System.Diagnostics.Debug.WriteLine("runGui started");

    // set up the main form, show it, hide this startup progress window

    System.Diagnostics.Debug.WriteLine("runGui finishing");
});

connectingTask.ContinueWith(connectFailAction,
                            System.Threading.CancellationToken.None,
                            TaskContinuationOptions.OnlyOnFaulted,
                            uiScheduler);

connectingTask.ContinueWith(postConnectAction,
                            System.Threading.CancellationToken.None,
                            TaskContinuationOptions.OnlyOnRanToCompletion,
                            uiScheduler)
              .ContinueWith(dbLoadingAction,
                            System.Threading.CancellationToken.None,
                            TaskContinuationOptions.None,
                            backgroundScheduler)
              .ContinueWith(runGuiAction,
                            System.Threading.CancellationToken.None,
                            TaskContinuationOptions.None,
                            uiScheduler);

如果连接失败,任务直接转到connectFailAction。

否则,在 UI 线程上,接下来运行 postConnectAction,同时在后台运行 dbLoadingAction。之后,runGuiAction 运行。

这大部分工作正常,但一个奇怪的边缘情况是CefSharp。具体来说,用户界面的非 CefSharp 部分“卡住”了——我无法调整表单大小,无法与任何控件交互,等等。

由于等效代码在 WinForms WebBrowser 控件上运行良好,我猜这部分是 CefSharp 的问题,但部分原因可能是我对 TPL 的错误使用/理解。

如果我按如下方式更改代码,UI 在加载过程中会卡住(不出所料),但 CefSharp 会正确启动。

connectingTask.ContinueWith(connectFailAction,
                            System.Threading.CancellationToken.None,
                            TaskContinuationOptions.OnlyOnFaulted,
                            uiScheduler);

connectingTask.Wait();
postConnectAction.Invoke(null);
dbLoadingAction.Invoke(null);
runGuiAction.Invoke(null);

如果(且仅当)使用 CefSharp,主线程卡在System.Windows.Threading.DispatcherSynchronizationContext.Wait,所以我猜上述任务之一永远不会干净地完成,导致事件队列无法正确刷新?

【问题讨论】:

  • 只是想确保您知道使用 await 后代码会更简单且明显正确。

标签: c# .net-4.0 task-parallel-library


【解决方案1】:

主线程卡在System.Windows.Threading.DispatcherSynchronizationContext.Wait,所以我猜测上述任务之一永远不会干净地完成,导致事件队列无法正确刷新?

问题在于后一个示例中的代码死锁。您正在调用Task,该Task 稍后会尝试在当前被connectingTask.Wait(); 阻止的UI 线程上运行延续。这就是为什么you shouldn't block on async code.

关于 CefSharp,您在连接后在 UI 线程上运行的延续可能是 CPU 密集型的,并导致您的 UI 消息循环停止超过合理的时间。由于您没有提供相关代码,因此无法确定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-28
    • 2015-06-25
    • 2022-01-08
    • 2011-08-07
    • 2011-09-26
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多