Thursday, September 11, 2008

Kill GeoMedia Process in GeoMedia .NET Application

While developing custom driving GeoMedia custom applications in .NET, I notice that even though references to GeoMedia COM objects - connections, GRecordsets, etc have been explicitly released at the end of the program, the GeoMedia process started by my application remains running in the background. It seems this is dependent on the GeoMedia objects and methods I used - sometimes the GeoMedia process terminate gracefully, sometimes not (especially if I use the GMLayout SmartFrames2D' CreateEmbed method).

I decided to put in explicit handling to kill the GeoMedia process in my class' constructor and destructor. The basic idea is to get the process id of my custom application's GeoMedia instance in the constructor. Then when exiting the custom application in the class' destructor, I use the process id to kill off the GeoMedia process.

The C# code listing below illustrates the basic idea.


using System;
using System.Collections;
using System.Diagnostics;
using GeoMedia = Intergraph.GeoMedia.GeoMedia;

class GeoMediaWrapper
{
private int _myProcessId; //my GeoMedia process id
private GeoMedia.Application _application;
public GeoMediaWrapper()
{
Type GeoMediaType;

//Build an array list of all running GeoMedia
//processes first

ArrayList processList = new ArrayList();
foreach (Process proc in Process.GetProcessesByName("GeoMedia"))
processList.Add(proc.Id);

//Start up an instance of GeoMedia
GeoMediaType = Type.GetTypeFromProgID("GeoMedia.Application", "localhost", true);
this._application = (GeoMedia.Application)Activator.CreateInstance(GeoMediaType);

//Loop through all the running GeoMedia processes.
//If the id does not exist in the previously built list,
//then this must be my
GeoMedia process id number.

foreach (Process proc in Process.GetProcessesByName("GeoMedia"))
if (!GMProcs.Contains(proc.Id))
{
//this is my GeoMedia process!
this._GMProcId = proc.Id;
}
}
~GeoMediaWrapper()
{
//Kill off my GeoMedia process
Process.GetProcessById(this._GMProcId).Kill();
}
//...etc...
}

No comments: