AnsweredAssumed Answered

Saving MXD Lock Problem

Question asked by AkhilP on Aug 22, 2012
Latest reply on Aug 23, 2012 by AkhilP
I am trying to create an mxd with some layers and then simply save it.
I am getting the following error:
"A lock violation has occurred. (Exception from HRESULT: 0x80030021 (STG_E_LOCKVIOLATION))"
My code is doing the following:

1. Creating an MXD
2. Opening the MXD
3. Adding layers to the map
4. Assigning colours/rendering the layers
5. Setting the area of interest (extent)
6. Replace contents <- I see a lot of people doing this, I don't see the purpose (could someone elaborate)
7. Save the MXD <- error occurs here
8. Close the MXD

My code is below.

        public static void CreateMapDocument(string mapDocumentPath, string inputIncidentName, List<ENTFeatureClassWithName> featureClassList)
            MapDocument aMapDocument = new MapDocument();
            string mxdPath = mapDocumentPath + inputIncidentName + "_Temp.mxd";

            IMapDocument pMapDocument = new MapDocumentClass();

            if (pMapDocument.get_IsMapDocument(mxdPath))
                IMap pMap = pMapDocument.get_Map(0);

                for (int i = featureClassList.Count -1 ; i >= 0; i--)
                    IFeatureLayer featureLayer = new FeatureLayer();                   
                    featureLayer.FeatureClass = featureClassList[i].AFeatureClass;
                    featureLayer.Name = featureClassList[i].LayerName;
                    IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)featureLayer;                    
                    BLLColourSchemes.DetermineActiveColourScheme(pGeoFeatureLayer, BLLCreateIncident.colourScheme);                   


                //string newMxdPath = mapDocumentPath + inputIncidentName + ".mxd";
                pMapDocument.ReplaceContents(pMap as IMxdContents);
                pMapDocument.Save(true, false);
            //BLLCreateIncident.temporaryMXD = mxdPath;

Note: If I uncomment the newMxdPath and the pMapDocument.SaveAs(newMxdPath), but comment out pMapDocument.Save(true, false), the SaveAs works perfectly. I get an MXD that I want. But the goal is not to have 2 MXD's, one called temp and the other without temp.

I have also put .Close() before .Save() and no difference, the code gives the same error on .Save().
All of my layers are coming from my GeoDatabase.

Kindly advise, thanks!