I created a map using C# and esri wpf.We have an Excel add-in application that can open my map code. After my queryTask execute completed method is called, I invoked a method to add graphics to map. I got the following error when adding graphics to graphic layer: graphicMapLayer.Graphics.Add(graphic);System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it. at System.Windows.Threading.Dispatcher.VerifyAccess()I found a solution by calling Dispatcher.Invoke e.g. this.Dispatcher.Invoke( new AddGraphicDelegate(AddGraphic), graphic);private void AddGraphic(Graphic g) { graphicMapLayer.Graphics.Add(g); }However, after I got the above problem fixed, I got another thread exception for another arcgis method as following:at System.Windows.Threading.Dispatcher.VerifyAccess() at System.Windows.DependencyObject.GetValue(DependencyProperty dp) at ESRI.ArcGIS.Client.Graphic.get_Visibility() at ESRI.ArcGIS.Client.GraphicsLayer.RenderGraphics(Double resolution, MapPoint canvasOrigin, Envelope env, Envelope bufferedExtent, IEnumerable`1 gfxs, Boolean isClustering, IRenderer renderer, Boolean interactive) at ESRI.ArcGIS.Client.GraphicsLayer.Draw(Envelope extent, Double resolution, MapPoint canvasOrigin, Boolean useTransitions) at ESRI.ArcGIS.Client.Map.loadLayerInView(Boolean useTransitions, Envelope drawExtent, Layer layer) at ESRI.ArcGIS.Client.Map.loadLayersInView(Boolean useTransitions, Envelope drawExtent) at ESRI.ArcGIS.Client.Map.panTo(Geometry geometry, Boolean skipAnimation) at ESRI.ArcGIS.Client.Map.zoomTo(Geometry geom, Boolean skipAnimation) at ESRI.ArcGIS.Client.Map.set_Extent(Envelope value)Please help! I don't know where is the right place to call Invoke() in this case that handles all arcgis calls.Thanks!
I checked with Morten and he explained that you are using the wrong Dispatcher. You will need to get to the root visual, and use its Dispatcher. You can try using Application.Current.MainWindow.Dispatcher.
Application.Current.MainWindow.Dispatcher.Invoke(new NullDelegate(Update), null);
DependencyObject dependencyObject = VisualTreeHelper.GetParent(element); if (dependencyObject == null) return element; while (VisualTreeHelper.GetParent(dependencyObject) != null) dependencyObject = VisualTreeHelper.GetParent(dependencyObject); rootVisual = dependencyObject as Visual;
Does it hit this line?I think the best way to find the parent of your Map control is to use VisualTreeHelper. If you do this in while loop until GetParent() returns null, you will have the Visual root and use its Dispatcher. Something like this if Map is not a descendant of your Application.Current.MainWindow: DependencyObject dependencyObject = VisualTreeHelper.GetParent(element); if (dependencyObject == null) return element; while (VisualTreeHelper.GetParent(dependencyObject) != null) dependencyObject = VisualTreeHelper.GetParent(dependencyObject); rootVisual = dependencyObject as Visual; You will be able to then use rootVisual.Dispatcher.
Tried that, got same exception when doing VisualTreeHelper.GetParent(element).
Do you have any other ideas after all of these solutions failed? You can tell me the big picture like where I should invoke the map container dispatcher or you can give me an example that worked on excel.Thanks!
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.