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
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.
–
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.
–
–
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.