2

我们的团队遇到了一个问题,即ContinueWith()在主机进程关闭之前无法运行。例如,下面的代码已经在生产环境中运行了一年。但是自从我们开始使用更新版本的框架后,我们就遇到了这个问题。

运行此代码时,this.Sites = GetViewableSites();运行,然后在调试时没有其他任何反应。点击 后F10,调试器返回 UI。只有在用户关闭 UI 后才会ContinueWith()执行。

this.PerformBusyAction(() =>
{
    this.Sites = GetViewableSites();
}, ViewModelResource.LoadingForm)
.ContinueWith(originatingTask =>
{
    // ** This doesn't execute until the user closes the UI **
    if (originatingTask.Exception == null)
    {
        PerformPostSuccessfulInitialization();
        return;
    }
    PerformPostUnsuccessfulInitialization(originatingTask.Exception.InnerExceptions);
});

为了完整起见,这里是PerformBusyAction()

    protected Task PerformBusyAction(Action action, string busyMessage)
    {
        this.BusyMessage = busyMessage;
        this.IsBusy = true;  // Let the UI know we're busy, so it can display a busy indicator

        var task = Task.Factory.StartNew(() =>
        {
            try
            {
                action();
            }
            finally
            {
                this.IsBusy = false;
                this.BusyMessage = String.Empty;
            }
        });

        return task;
    }

知道什么会导致这种情况吗?我们可以说罪魁祸首是我们的框架,它可能是,但我们无法弄清楚这怎么可能发生。为了解决这个问题,我们刚刚停止使用ContinueWith()并将所有代码放在第一部分/任务中。

编辑

也许这是一个错误?这里有一个类似的问题,尽管它与Console.ReadKey().

编辑 2 - 调用 GetViewableSites() 后立即调用堆栈

未标记 > 6324 6 工作线程 工作线程 Acme.Mes.Apps.Derating.ViewModels.Controls.GlobalTabControlViewModel.Initialize.AnonymousMethod__9 正常 Acme.Mes.Apps.Derating.ViewModels.dll!Acme.Mes.Apps.Derating.ViewModels.Controls .GlobalTabControlViewModel.Initialize.AnonymousMethod__9() 第 361 行
Acme.Mes.Client.dll!Acme.Mes.Client.Mvvm.MvvmTools.ViewModelBase.PerformBusyAction.AnonymousMethod__c() 第 494 行 + 0xe 字节
mscorlib.dll!System.Threading.Tasks。 Task.InnerInvoke() + 0x49 字节
mscorlib.dll!System.Threading.Tasks.Task.Execute() + 0x2e 字节 mscorlib.dll
!System.Threading.Tasks.Task.ExecutionContextCallback(object obj) + 0x15 字节
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) + 0xa7 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading .ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) + 0x16 字节
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) + 0xcb 字节
mscorlib.dll !System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) + 0xb3 字节
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x7 字节
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x149 字节
mscorlib.dll!System.Threading._ThreadPoolWaitCallback。 PerformWaitCallback() + 0x5 字节
[本机到托管转换]

4

1 回答 1

0

尝试使用调度程序访问 IsBusy 和 BusyMessage。

Dispatcher.BeginInvoke((Action) (() =>
    {
        this.IsBusy = false;
        this.BusyMessage = String.Empty;
    }));
于 2013-10-02T04:45:54.427 回答