private void dataAddedWorker_DoWork(object sender, DoWorkEventArgs args) { var worker = sender as BackgroundWorker; //going to modify the list so can't use a foreach. while (true) { if (layerDataToProcess.Count == 0) { break; } VectorLayerData vectorLayerData = layerDataToProcess[0]; if (worker != null && worker.CancellationPending) { args.Cancel = true; break; } StyleSpecification style = Styles[cDefaultStyleID]; string styleID = (!string.IsNullOrEmpty(vectorLayerData.Style)) ? vectorLayerData.Style : LayerDefinition.LayerStyleName; if (Styles.ContainsKey(styleID)) { style = Styles[styleID]; } var wkbReader = new WKBReader(new GeometryFactory(new PrecisionModel(), 4326)); try { IGeometry geo = wkbReader.Read(vectorLayerData.Geo); VectorLayerData data = vectorLayerData; Dispatcher.BeginInvoke(delegate { var infogeo = new InfoGeometry(geo, style, MapInstance, this) { Label = data.Label, LabelVisibility = (LayerDefinition.LabelOn) ? Visibility.Visible : Visibility.Collapsed, ItemID = data.ID, EnableBalloon = EnableBalloon, EnableSelection = EnableSelection, TimeStamp = data.TimeStamp, }; infogeo.Balloon += (o, e) => { if ( ((InfoGeometry)o).BalloonData.Count == 0) { Commands.LoadBalloonDataCommand.Execute(e, o); } }; infogeo.Selected += (o, e) => Commands.ItemSelectedCommand.Execute(o); if (LayerDefinition.PermissionToEdit) { infogeo.Selected += (o, e) => Commands.EditVectorCommand.Execute(o); } mapLayerGeometries.Add(infogeo); }); } catch (Exception e) { Debug.WriteLine(e.Message); } layerDataToProcess.Remove(vectorLayerData); } }