添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How do I fix the "System.Threading.Tasks.TaskCanceledException: 'A task was canceled.'" exception on app exit (Winforms)?

Ask Question

I have a WinForms MP3 player desktop app (.NET Framework 4.7.2) that is using ElementHost to host a MediaElement control and has a DispatcherTimer to control playback (such as updating a Slider).

Everything works fine, but when I exit the app, I get the "System.Threading.Tasks.TaskCanceledException: 'A task was canceled.'" exception (I notice it only when running under the debugger).

It's nothing but a nuisance and feels mostly harmless, but I don't like exceptions that I don't understand. The callstack is not super useful:

>   mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task task)   Unknown
    mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task task)  Unknown
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Wait(System.TimeSpan timeout)  Unknown
    WindowsBase.dll!System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherOperation operation, System.Threading.CancellationToken cancellationToken, System.TimeSpan timeout)   Unknown
    WindowsBase.dll!System.Windows.Threading.Dispatcher.Invoke(System.Action callback, System.Windows.Threading.DispatcherPriority priority, System.Threading.CancellationToken cancellationToken, System.TimeSpan timeout) Unknown
    WindowsBase.dll!MS.Internal.WeakEventTable.OnShutDown() Unknown
    WindowsBase.dll!MS.Internal.WeakEventTable.WeakEventTableShutDownListener.OnShutDown(object target, object sender, System.EventArgs e)  Unknown
    WindowsBase.dll!MS.Internal.ShutDownListener.HandleShutDown(object sender, System.EventArgs e)  Unknown

Is this happening because the WPF stack does not get disposed properly somehow? I ensure my DispatchTimer is stopped in MainForm_FormClosing but perhaps there is something else I need to clean up?

Not a critical issue, of course, by annoying.

Maybe add one of the many unhandled exception handlers. That or clear out all the first chance setting options in Debug > Windows > Exception settings – user585968 Nov 22, 2021 at 1:58

This Exception is thrown when an asynchronous method is not allowed to run to completion - in order that other asynchronous methods with the same CancellationToken can stop processing gracefully if needed.

You can likely ignore it, as it seems to be an uncaught exception from one of those libraries you're using. If it really bothers you and you know it's not in your codebase, you could soak the Exception, but this is generally not considered good practice.

Is there a good way to find which method is causing this exception to be thrown? I'd feel better if I knew which library is causing it. The callstack at exception throwing time is not very useful, and given that it's app shutdown, there are very few running threads left. – David Airapetyan Nov 22, 2021 at 0:53 You could wrap your code with a catch clause, and put a breakpoint on it to see if you can locate the offender in the call stack. Unfortunately, if one of your libraries has caught an Exception and re-thrown a new CancellationException, you won't get the full history, and your best bet then is to see if you can dive into the source of the library if it's available. – Chris Nov 22, 2021 at 1:02

It looks like an issue with .NET Framework 4.7.2. The problem and a workaround are described here: TaskCanceledException in ShutDownListener.

The tl'dr is that adding the following to my App.config made the exception go away:

  <runtime>
    <AppContextSwitchOverrides value="Switch.MS.Internal.DoNotInvokeInWeakEventTableShutdownListener=true"/>
  </runtime>
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.