Access a Windows Form while in a QueuedTask

308
0
01-04-2018 05:54 AM
BrianBulla
Occasional Contributor III

I need to access a ListBox control while also performing an action requiring a QueuedTask.  When access the SelectedItems on the ListBox, I get an error about the ListBox being access outside of the thread that created it.

What are the best ways to get around this?  Should I just save the SelectedItems to a List before entering the QueuedTask, or is there a way to access Form controls while in the QueuedTask??

QueuedTask.Run(() =>
                {
                    Map map = MapView.Active.Map;

                    foreach (string layerName in lstLayers.SelectedItems)    //This is where I get the error
                    {
                        FeatureLayer currentLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name.IndexOf(layerName, StringComparison.CurrentCultureIgnoreCase) >= 0).FirstOrDefault();
                        currentLayer.ClearSelection();
                    }                    
                }
                );  //end of Queued Task
0 Kudos
0 Replies