O
Oleg Ogurok
Hi all,
I wrote a Windows service that periodically creates a COM instance a calls a
method of it. All of it is done through Reflection because I don't know the
names of the COM object/method at compile time.
I notice that when I build a newer version of a COM dll, when I try to copy
it to the destination dir, I get an error "File is in use by another
process." I know for sure that it's my Windows Service using it because once
I stop it, I can install the new COM dll.
What do I have to do in order to dispose the COM instance?
Here's the code of my launching method.
public void Launch()
{
Type type = Type.GetTypeFromProgID(programName);
if (type == null)
{ throw new LaunchException("Can't find program " + programName + ",
perhaps it's not registered");
}
try
{
object instance = Activator.CreateInstance(type);
type.InvokeMember(methodName,
BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.InvokeMethod,
null, instance, arguments);
//TODO COM object remains in memory, need to dispose.
}
catch (Exception e)
{
throw new LaunchException("Error running COM method " + methodName + "
in class " + programName, e);
}
catch
{
string error = "Error running COM method " + methodName + " in class " +
programName;
throw new LaunchException(error);
}
}
I wrote a Windows service that periodically creates a COM instance a calls a
method of it. All of it is done through Reflection because I don't know the
names of the COM object/method at compile time.
I notice that when I build a newer version of a COM dll, when I try to copy
it to the destination dir, I get an error "File is in use by another
process." I know for sure that it's my Windows Service using it because once
I stop it, I can install the new COM dll.
What do I have to do in order to dispose the COM instance?
Here's the code of my launching method.
public void Launch()
{
Type type = Type.GetTypeFromProgID(programName);
if (type == null)
{ throw new LaunchException("Can't find program " + programName + ",
perhaps it's not registered");
}
try
{
object instance = Activator.CreateInstance(type);
type.InvokeMember(methodName,
BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.InvokeMethod,
null, instance, arguments);
//TODO COM object remains in memory, need to dispose.
}
catch (Exception e)
{
throw new LaunchException("Error running COM method " + methodName + "
in class " + programName, e);
}
catch
{
string error = "Error running COM method " + methodName + " in class " +
programName;
throw new LaunchException(error);
}
}