GPU mode: RequestMapRefresh calls MapView.RefreshAsync() from worker threads (unobserved InvalidOperationException), 15.0.0-beta197

Hi Team,

With GpuBasemap enabled (ThinkGeo.UI.Wpf / ThinkGeo.Gpu 15.0.0-beta197), our log fills with unobserved task exceptions. They appear after we add a layer or toggle WMS/WFS layers. Dozens arrive in bursts whenever the finalizer runs:

System.AggregateException: A Task's exception(s) were not observed ...
 ---> System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.<VerifyAccess>g__ThrowVerifyAccess|7_0()
   at ThinkGeo.UI.Wpf.MapViewBase.A(IEnumerable`1, CancellationToken)
   at ThinkGeo.UI.Wpf.MapViewBase.RefreshAsync(OverlayRefreshType refreshType, CancellationToken cancellationToken)

Our own RefreshAsync calls are all awaited on the UI thread. When we inspected the beta197 assemblies, we found the following:

  • The GPU overlay that GpuBasemap attaches implements IGpuMapHost.RequestMapRefresh() as MapView.RefreshAsync() . The task is not awaited, and the call is not marshalled to the map’s dispatcher.
  • GpuMapController calls RequestMapRefresh from:
    • RefreshVectorTiles , which also runs from the FeatureSourceVectorTileSource.DataChanged handler, so on whichever thread raised that event;
    • RefreshRasterTiles ;
    • style application;
    • the coverage watchdog.
  • RefreshAsync has no thread check of its own. An off-thread call therefore proceeds until MapViewBase reads an overlay’s Parent while opening overlays. It then throws, the refresh is lost, and the exception only surfaces later through TaskScheduler.UnobservedTaskException .

Other host callbacks already go through TryPostToUi , and GpuBasemap marshals its automatic restyle to the dispatcher. Could RequestMapRefresh do the same, for example Dispatcher.InvokeAsync(() => map.RefreshAsync()) , and observe or log the resulting task?

We can’t work around this on our side. RefreshAsync is not virtual, and the exception happens before DrawAsyncCore runs, so a MapView subclass never sees the call.

We haven’t yet pinned down which controller path runs off the UI thread in our application.

Thanks!

Hi Julian,

You read it right, and it is fixed in beta203.

The WPF host now runs RequestMapRefresh on the dispatcher and observes the task: a cancellation (a newer refresh superseded it) is dropped, anything else is written to the trace.

The paths that ran off the UI thread were a cut source’s DataChanged (raised on the thread that changes the data), a raster source’s Refreshed (raised when a fetch completes), and GpuBasemap.RefreshVectorTiles called from a worker. RefreshVectorTiles and RefreshRasterTiles now post themselves to the UI thread, so all three land there; you may call RefreshVectorTiles from any thread.

Also found and fixed: a GpuBasemap created with new GpuBasemap(style) did not watch its sources for DataChanged until the first SetStyleAsync.

MapView.RefreshAsync is unchanged - the map view is called on its UI thread, like any WPF control, which is what your code does.

Thanks,
Ben