arcgis wpf: QueryTask Crashes on query completed net 4.0 big solution

Discussion created by dimangulov on Feb 4, 2014
Latest reply on Feb 17, 2014 by MBranscomb-esristaff
Product: ArcGis 10.2 WPF Runtime

Hi, I'm using Featurelayer. On computers with NET 4.5 everything works fine. But computers NET 4.0 is an error.

Catching Unhandled exception
System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
at ESRI.ArcGIS.Client.GraphicsLayer.<Graphics_Collectionchange d>b__1(Graphic a)
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ESRI.ArcGIS.Client.GraphicsLayer.Graphics_Collectionchange d(Object sender, NotifyCollectionchange dEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionchange d(NotifyCollectionchange dEventArgs e)
at ESRI.ArcGIS.Client.GraphicCollection.AddRange(IEnumerable`1 items)
at ESRI.ArcGIS.Client.FeatureLayer.task_QueryComplete(Object sender, QueryEventArgs args)
at ESRI.ArcGIS.Client.Tasks.QueryTask.OnExecuteCompleted(QueryEventArgs args)
at ESRI.ArcGIS.Client.Tasks.QueryTask.<>c__DisplayClassf.<ExecuteAsync>b__d(Task`1 result)
at ESRI.ArcGIS.Client.Tasks.TaskBase.<>c__DisplayClassc`1.<HandeEventBasedTask>b__a(Task`1 result)
at System.Threading.Tasks.Task`1.<>c__DisplayClass17.<ContinueWith>b__16(Object obj)
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()

and so on ...

The most interesting cases looked similar to the network, I'm not the first to face such a problem, but to repeat it in a small decision can not.

Disassembled assembly ArcGis proved that there QueryTask class call the base class.

public void ExecuteAsync(Query query, object userToken = null)
            Action<Task<QueryResult>> isFaulted = (Task<QueryResult> result) => {
                if (result.IsFaulted)
                    this.LastResult = null;
                this.LastResult = result.Result.FeatureSet;
                QueryEventArgs queryEventArg = new QueryEventArgs(result.Result.FeatureSet, result.Result.ExceededTransferLimit, userToken);
            base.HandeEventBasedTask<QueryResult>((CancellationToken ct) => this.ExecuteTaskAsync(query, ct), Resources.QueryTask_Error, isFaulted, userToken);

HandeEventBasedTask turns out he is responsible for returning to the desired flow! and what we see inside the method

taskToExecute(token).ContinueWith((Task<R> result) => {
                    ... some code
                }, TaskScheduler.FromCurrentSynchronizationContext());

To return to the calling thread is used method TaskScheduler.FromCurrentSynchronizationContext (), but there's a bug in NET 4.0, NET 4.5 and all perfectly fulfills.

Help please, you only need to change this piece of code that all worked perfectly in both fremvork. If I need help with those kind of logic there references.

!!! reproduced