Can't access graphics layer from asynchronous operation

Discussion created by robertrower on Apr 24, 2014
Latest reply on Apr 28, 2014 by MBranscomb-esristaff
Hi, I spend whole week and still I can't find the solution. In my code, button click runs long asynchronous operation. After calculation I want add results (which type is List<Graphic>) to my GraphicsLayer.

Using ContinueWithMethod and TaskScheduler or Dispatcher works fine for all UI elements for example ProgressBar but not for my resultsGraphicsLayer. Why?

Exception: "An exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll but was not handled in user code.
The calling thread cannot access this object because a different thread owns it."

private void runButton_Click(object sender, RoutedEventArgs e)
 var _ = RunCalcAsync();

private async Task RunCalcAsync()
 await Task.Run(() =>
  \\ long time operation
  var results = RunCalc();

 }).ContinueWith((t) =>
   GraphicsLayer resultsGraphicsLayer = map.Layers["results"] as GraphicsLayer;
   resultsGraphicsLayer.Graphics.AddRange(results); // this line throw an exception :-(

Any help is much appreciated. Thanks!