Saving MXD Lock Problem

2604
2
Jump to solution
08-22-2012 11:49 AM
Highlighted
New Contributor III
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.

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

            IMapDocument pMapDocument = new MapDocumentClass();

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

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

                SetDataFrameACustomFullExtent(pMapDocument);

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

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!
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Regular Contributor III
You're using aMapDocument to create the mxd then using pMapDocument to open that mxd and make changes to it.  When you try to save using pMapDocument, you're getting the lock error because aMapDocument still has the file open.  Use one or the other, but not both.

View solution in original post

Reply
0 Kudos
2 Replies
Highlighted
Regular Contributor III
You're using aMapDocument to create the mxd then using pMapDocument to open that mxd and make changes to it.  When you try to save using pMapDocument, you're getting the lock error because aMapDocument still has the file open.  Use one or the other, but not both.

View solution in original post

Reply
0 Kudos
Highlighted
New Contributor III
Thanks Neil, stupid mistake on my part.
Reply
0 Kudos