Home > database >  How do I fix the "System.Threading.Tasks.TaskCanceledException: 'A task was canceled.'
How do I fix the "System.Threading.Tasks.TaskCanceledException: 'A task was canceled.'

Time:11-22

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.

CodePudding user response:

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.

CodePudding user response:

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>
  • Related