Select to view content in your preferred language

FeatureSet.ToJson() must now be called on the UI thread in 2.3

1017
2
12-20-2011 04:55 PM
RyanCooney
Occasional Contributor
As of version 2.3 FeatureSet.ToJson() throws an exception if you call it from a background thread. This was not the case in 2.2. Was this an intended change?

System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at ESRI.ArcGIS.Client.Graphic.get_Layer()
   at ESRI.ArcGIS.Client.Tasks.Utils.JSON.ArcGISJsonWriter.GraphicsToFeatureServiceJson(IEnumerable`1 graphics, Boolean normalize, Boolean onlyDirty, List`1 fields)
   at ESRI.ArcGIS.Client.Tasks.FeatureSet.ToJson(Boolean includeGeometryInJson, Boolean normalize)
   at ESRI.ArcGIS.Client.Tasks.FeatureSet.ToJson()
0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor
Sorry, this wasn't intended and we'll have an update in a future release.
As a work-around for now, set the Fields property on the FeatureSet, prior to calling the worker thread, and you should be good to go.

Example:
FeatureSet fs = new FeatureSet()
{
 Fields = new List<Field>() //or FeatureLayer.LayerInfo.Fields
};
fs.Features.Add(new ESRI.ArcGIS.Client.Graphic() { Attributes = { { "ID", "1" } }, Geometry = new ESRI.ArcGIS.Client.Geometry.MapPoint(1, 2) });
System.ComponentModel.BackgroundWorker worker = new System.ComponentModel.BackgroundWorker();
worker.DoWork += (a, b) =>
 {
  FeatureSet fs2 = (FeatureSet)b.Argument;
  b.Result = fs2.ToJson();
 };
worker.RunWorkerCompleted += (a, b) =>
 {
   MessageBox.Show(b.Result.ToString());
 };
worker.RunWorkerAsync(fs);


Sorry for the inconvenience this might have caused you.
0 Kudos
RyanCooney
Occasional Contributor
Thanks for the quick reply. I've implemented the workaround and it does the trick.
--Ryan
0 Kudos