Add Layer to Map from other Thread Context

Discussion created by nupplaphil on Oct 4, 2011
Hi All,

Is it possible to call this Event from another Thread than the main one ? I do make some calculations in a extra thread and when it's finished, I want to automatically add the Layer to ArcMap. At the moment, I've to press a Button to Add the Layer manually because ArcMap throw a unhandled Exception when the Thread automatically trys to add the Layer.

void pArcMAI_OnAddLayers(object sender, ArcMAIEventArgs e)
            IWorkspaceFactory pWorkspaceFactory;
            IWorkspace pFeatureWorkspace;
            IFeatureLayer pFeatureLayer;
            IMxDocument pMxDocument;
            IMap pMap;

            if (e.File == "")

            // Create a new ShapefileWorkspaceFactory object and open a shapefile folder
            pWorkspaceFactory = new AccessWorkspaceFactoryClass();            
            pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(e.File, 0);
            //Create a new FeatureLayer and assign a shapefile to it            

            IEnumDataset pEnumDataset = pFeatureWorkspace.Datasets[esriDatasetType.esriDTFeatureClass];
            IDataset pDataset;

            while ((pDataset = pEnumDataset.Next()) != null)
                pFeatureLayer = new FeatureLayerClass {FeatureClass = pDataset as IFeatureClass};
                pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                //Add the FeatureLayer to the focus map
                pMxDocument = ((IApplication)m_hookHelper.Hook).Document as IMxDocument;
                pMap = pMxDocument.FocusMap;

Any ideas why ? Any ideas how I can solve the Problem ?