wpf 关闭应用程序时 WindowsBase.dll 中发生“System.Threading.Tasks.TaskCanceledException”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26613956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:39:32  来源:igfitidea点击:

'System.Threading.Tasks.TaskCanceledException' occurred in WindowsBase.dll when closing application

c#wpfmultithreadingcaliburn.micro

提问by Dean

I have this property in my viewmodel.

我的视图模型中有这个属性。

public bool IsKWH
{
    get { return _isKwh; }
    set
    {
        if (value.Equals(_isKwh)) return;
        _isKwh = value;
        NotifyOfPropertyChange(() => IsKWH);
    }
}

Sometimes (~1 in 10 times) when I close down my application I get the following error in NotifyOfPropertyChange:

有时(大约 10 次中有 1 次)当我关闭我的应用程序时,我会在 NotifyOfPropertyChange 中收到以下错误:

An exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in WindowsBase.dll but was not handled in user code

WindowsBase.dll 中出现“System.Threading.Tasks.TaskCanceledException”类型的异常,但未在用户代码中处理

Additional information: A task was canceled.

附加信息:任务已取消。

I have a System.Threading.Timer in my view model that is making a webservice call to update this and many other properties.

我的视图模型中有一个 System.Threading.Timer 正在调用 web 服务来更新这个和许多其他属性。

I am using Caliburn.Micro and it seems to have started happening when I updated from 1.5 to 2.0.

我正在使用 Caliburn.Micro,当我从 1.5 更新到 2.0 时,它似乎已经开始发生。

Is there anyway to prevent this error from occurring?

有没有办法防止这个错误的发生?

回答by Digitalsa1nt

It is possible that intermittently your application fails to dispose of any secondary threads that it is using before the application closes. This can often cause an error message such as the one you posted. Could I suggest trying the following:

您的应用程序可能会间歇性地无法在应用程序关闭之前处理它正在使用的任何辅助线程。这通常会导致出现错误消息,例如您发布的消息。我可以建议尝试以下方法:

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
       // close all active threads
       Environment.Exit(0);       
}

This should force the application to terminate all active threads before it closes. I recall having a similar problem and that particular little fix solved it. Might be worth giving it a try, let me know if it doesn't help and we can see what other solutions there might be. Hope this works for you.

这应该强制应用程序在关闭之前终止所有活动线程。我记得有一个类似的问题,那个特别的小修复解决了它。可能值得一试,如果没有帮助,请告诉我,我们可以看看还有哪些其他解决方案。希望这对你有用。

回答by Marteen

FWIW, that fix didn't help me. the problem was coming from a 3rd party DLL that's dynamically loaded. I was unable to stop the exception from getting thrown, however I ignore the exception in the app exception handler:

FWIW,那个修复对我没有帮助。问题来自动态加载的第 3 方 DLL。我无法阻止抛出异常,但是我忽略了应用程序异常处理程序中的异常:

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(
                (sender2, args) =>
                {
                    Exception ex = (Exception)args.ExceptionObject;  
                    // unloading dragon medical one
                    if (ex is TaskCanceledException)  
                        return;  // ignore