TaskCanceledException on shutdown

5599
5
Jump to solution
02-28-2018 09:27 AM
ChrisSmith12
New Contributor II

I'm getting a TaskCanceledException when I shut down my application. Stack trace below. I'm using 100.2.1 on Windows 10 x64. In my current scenario I have one basemap layer (local tpk file) and a few graphics overlays that contain graphics that are being updated at ~1Hz.

Thoughts on what may be causing this or how to prevent it? Thank you.

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Windows.Threading.DispatcherOperation.Wait(TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.Invoke(Action callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.Invoke(Action callback)
   at Esri.ArcGISRuntime.UI.Controls.GeoView.RuntimeCoreNet.ICoreCallback_GeoView_LockSurface.UnlockSurfaceRequested(Boolean wasResized)
   at RuntimeCoreNet.CoreDxDevice.OnPrivateCoreGeoView_UnlockSurfaceRequested(Boolean wasResized)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
GregDeStigter
Esri Contributor

That looks OK Chris.

The cancelled tasks are from the MapView's internal draw requests that get dispatched to the primary UI thread. When the app closes and the Dispatcher is shutdown any of these DispatcherOperations still in the queue are aborted and throw the TaskCanceledException that you're seeing. Being first-chance exceptions these don't necessarily signify a bug as they're likely handled later. You can safely allow the debugger to ignore these by turning off these exceptions in Visual Studio's Exception Settings.

That said, your work-around should also function. My theory on why it doesn't work is that there's not enough time between removing the MapView and shutting down the Dispatcher for the app to clear the dispatcher queue - so some operations still get aborted / canceled. No guarantees, but you could try adding this code after removing your MapView to give the app a little extra time to flush the queue:

Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, new Action(() => { }));

View solution in original post

0 Kudos
5 Replies
GregDeStigter
Esri Contributor

I've seen an exception like this when the GeoView is shutdown while a draw is still active. This could happen in situations like yours where you need the map to constantly redraw. We're working on some changes to this area of the codebase that should alleviate the problem in the next version.

Usually an exception at close isn't a huge problem because the app is exiting anyway so (depending on your app) the exception can be ignored. But if you want a workaround to get rid of the exception, you'll need to suspend map drawing before your app fully closes. The way to do that is to unload the GeoView (MapView / SceneView) - just take it out of the visual tree in the closing code of your app. This will suspend map rendering and allow the final update to finish instead of throwing the exception.

0 Kudos
ChrisSmith12
New Contributor II

Thanks Greg.

0 Kudos
ChrisSmith12
New Contributor II

Greg,

   I have tried the following in the code behind that holds the Esri MapView. I verified that the MapView is getting removed from the visual tree, however, I still get the TaskCanceledException on shutdown. Note: there are other places that hold references to the MapView. Any ideas? Thanks

if (Application.Current.MainWindow != null)
{
  Application.Current.MainWindow.Closing += (sender, args) =>
  {
    MyMapView.GetVisualParent<System.Windows.Controls.Grid>().Children.Remove(MyMapView);
  };
}
0 Kudos
GregDeStigter
Esri Contributor

That looks OK Chris.

The cancelled tasks are from the MapView's internal draw requests that get dispatched to the primary UI thread. When the app closes and the Dispatcher is shutdown any of these DispatcherOperations still in the queue are aborted and throw the TaskCanceledException that you're seeing. Being first-chance exceptions these don't necessarily signify a bug as they're likely handled later. You can safely allow the debugger to ignore these by turning off these exceptions in Visual Studio's Exception Settings.

That said, your work-around should also function. My theory on why it doesn't work is that there's not enough time between removing the MapView and shutting down the Dispatcher for the app to clear the dispatcher queue - so some operations still get aborted / canceled. No guarantees, but you could try adding this code after removing your MapView to give the app a little extra time to flush the queue:

Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, new Action(() => { }));
0 Kudos
ChrisSmith12
New Contributor II

That did the trick Greg. Thank you very much.

0 Kudos