gpt4 book ai didi

c# - ContinueWith() 直到主机进程结束才执行

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:38 25 4
gpt4 key购买 nike

我们的团队遇到了一个问题,即 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() 并将所有代码放在第一部分/任务中。

编辑

也许这是一个错误?还有类似的问题here ,尽管它与 Console.ReadKey() 一起使用。

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

Not Flagged > 6324 6 Worker Thread Worker Thread Acme.Mes.Apps.Derating.ViewModels.Controls.GlobalTabControlViewModel.Initialize.AnonymousMethod__9 Normal Acme.Mes.Apps.Derating.ViewModels.dll!Acme.Mes.Apps.Derating.ViewModels.Controls.GlobalTabControlViewModel.Initialize.AnonymousMethod__9() Line 361
Acme.Mes.Client.dll!Acme.Mes.Client.Mvvm.MvvmTools.ViewModelBase.PerformBusyAction.AnonymousMethod__c() Line 494 + 0xe bytes
mscorlib.dll!System.Threading.Tasks.Task.InnerInvoke() + 0x49 bytes
mscorlib.dll!System.Threading.Tasks.Task.Execute() + 0x2e bytes
mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj) + 0x15 bytes
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x16 bytes
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) + 0xcb bytes
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) + 0xb3 bytes
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x7 bytes
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x149 bytes
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() + 0x5 bytes
[Native to Managed Transition]

最佳答案

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

Dispatcher.BeginInvoke((Action) (() =>
{
this.IsBusy = false;
this.BusyMessage = String.Empty;
}));

关于c# - ContinueWith() 直到主机进程结束才执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19125068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com