Working with a Map Document causes my program to freeze.

Discussion created by Thomas_Bates on Feb 16, 2011
Latest reply on Feb 22, 2011 by Thomas_Bates
First off, I'm using Delphi 2007 and ArcObjects 9.3.

My application has a bit of start-up code that loads a map document, updates the feature layers to make sure they are looking at the correct File GeoDatabase, and saves the map document again.  Then, the updated map document gets loaded into the map control and everything looks beautiful.

THE CATCH is that this works sometimes, but not all the time.  When I run it from the Delphi IDE, it works about 99% of the time.  When I run the executable file directly, it fails about 90% of the time.

When it fails, it doesn't throw an exception or anything, it just freezes.  And it freezes in random places:  sometimes in the middle of the "Save" call, the "Close" call, the "Get_UsesRelativePaths" call, and sometimes while leaving the method that contains the ArcObjects calls.  And sometimes not at all.

I created a little stand-alone application with just the bit of start-up code, and it shows the same behaviour.  I then started removing bits to try to isolate the cause. 

I started by commenting out the code that modifies the feature layers.  It still froze.  Then I continued until the code was only opening the MapDocument, accessing the Map and then closing the document again.  It still froze.  I then commented out the Get_Map call and after repeated tries I could not get it to freeze when it was just opening and closing the map document.

Here is the method that I'm working with:

procedure TMain.UpdateMapDocument(AMapDocumentFileName, AWorkspacePath: string);
  mapDocument:      IMapDocument;
  isMapDocument:    WordBool;
  map:              IMap;
  useRelativePaths: WordBool;
  Status('Updating Map Document ...');

  if FileExists(AMapDocumentFileName) and DirectoryExists(AWorkspacePath) then

    Status('Creating Map Document');
    mapDocument := CoMapDocument.Create as IMapDocument;

    Status('Testting Map Document');
    mapDocument.Get_IsMapDocument(AMapDocumentFileName, isMapDocument);
    if isMapDocument then
      Status('Opening Map Document');
      mapDocument.Open(AMapDocumentFileName, '');

      Status('Opening Map');
      mapDocument.Get_Map(0, map);

      //  Status('Updating Feature Layer Connections');
      //  UpdateFeatureLayerConnections(AWorkspacePath, map);

      Status('Saving Map');
      mapDocument.ReplaceContents(map as IMxdContents);

      Status('Getting Relative Units');

      Status('Saving Map Document');
      mapDocument.Save(useRelativePaths, False);

      Status('Closing Map Document');

  Status('... Updated Map Document');

I've also attached the whole little test project.

When it works, it works exactly as expected so it seems that I'm doing this correctly.

I'm hoping someone can see something that I'm doing wrong that may be causing the freezing, or that someone has seen this freezing before and knows a work-around to avoid it.


Thomas Bates.