Problem with layers order

460
1
07-02-2013 10:01 AM
LuisGarcia2
Occasional Contributor II
I have an application where clients add layers to the map by selecting from a series of services we have available in our company. The user can save the state of the application and open the file again. When the user re-opens the file the code loops through the existing layers and and upload them using this code:

                           
ArcGISDynamicMapServiceLayer dLayer = new ArcGISDynamicMapServiceLayer();

dLayer.Url = layer.URL; //we only save the metadata of the layer for other engineering reasons so we instantiate the object, upload the URL and initialize the layer.

dLayer.InitializationFailed += new EventHandler<EventArgs>(layer_InitializationFailed);
dLayer.Initialized += (a, b) =>
{
     mapControl1.map1.Layers.Insert(1, dLayer);
};
dLayer.Initialize();


So that adds the layers always in the same position in which the user added it... right? ... well not quite. The problem is that the initialization procedure, although fast, does not end necessarily at the same time; so whichever layer is done first is the one that goes in first to the map changing the position the user added the layers in.
So, the question is, how can we handle that? I guess at that point we are working with different threads and we need to make them wait? Is there a way around this?

Thanks!
0 Kudos
1 Reply
DominiqueBroux
Esri Frequent Contributor
I think you could keep the current position before calling Initialize.Something like:
var pos = mapControl1.map1.Layers.Count;
dLayer.Initialized += (a, b) =>
{
     mapControl1.map1.Layers.Insert(pos, dLayer);
};
dLayer.Initialize();
0 Kudos