C
Christopher Carnahan
I need to figure out how to terminate a thread while it is blocked trying to
create a COM object via interop.
In a worker thread, I do something like this:
Type t = null;
Object activatedObject = null;
Legacy.IScheduled comObject = null;
t = Type.GetTypeFromProgID(ProgID);
activatedObject = Activator.CreateInstance(t);
comObject = (Legacy.IScheduled) activatedObject;
t.InvokeMember("Execute", BindingFlags.InvokeMethod, null,
activatedObject,
while (Marshal.ReleaseComObject(comObject) > 0);
comObject = null;
while (Marshal.ReleaseComObject(activatedObject) > 0);
activatedObject = null;
But the COM object I am calling is an out-of-process COM server written in
VB. If the .exe is busy or hung, the call to Activator.CreateInstance never
returns. I need to kill the worker thread if it hangs on this line, but
Thread.Abort doesn't work, since the ThreadAbortException isn't raised while
the thread is busy.
In .Net 1.1, I did this:
Thread t = new Thread(new ThreadStart(_thread.Abort));
t.Start();
Thread.Sleep(1000);
if (null != _thread)
{
_thread.Abort();
_thread = null;
}
_thread is the worker thread I need to kill. I spawned off a new thread
that called abort on the worker thread, waited a second, and called abort
from the main thread. This is super ugly, but it seemed to work, because
the worker thread would die. But when I recompiled my code for .Net 2.0,
this quit working. The call to _thread.Abort never returns, presumably
because it is blocking, waiting for the worker thread to stop blocking on
the call to the Activator.
So, any ideas how to do this? What I really need is a timeout mechanism on
the call to Activator.CreateInstance, but there isn't one. Is there another
way to approach this? I could convert the worker thread to a worker
process, and just kill the process, or do something with AppDomains, but I
was hoping for a less dramatic solution. Thoughts?
- Christopher
create a COM object via interop.
In a worker thread, I do something like this:
Type t = null;
Object activatedObject = null;
Legacy.IScheduled comObject = null;
t = Type.GetTypeFromProgID(ProgID);
activatedObject = Activator.CreateInstance(t);
comObject = (Legacy.IScheduled) activatedObject;
t.InvokeMember("Execute", BindingFlags.InvokeMethod, null,
activatedObject,
while (Marshal.ReleaseComObject(comObject) > 0);
comObject = null;
while (Marshal.ReleaseComObject(activatedObject) > 0);
activatedObject = null;
But the COM object I am calling is an out-of-process COM server written in
VB. If the .exe is busy or hung, the call to Activator.CreateInstance never
returns. I need to kill the worker thread if it hangs on this line, but
Thread.Abort doesn't work, since the ThreadAbortException isn't raised while
the thread is busy.
In .Net 1.1, I did this:
Thread t = new Thread(new ThreadStart(_thread.Abort));
t.Start();
Thread.Sleep(1000);
if (null != _thread)
{
_thread.Abort();
_thread = null;
}
_thread is the worker thread I need to kill. I spawned off a new thread
that called abort on the worker thread, waited a second, and called abort
from the main thread. This is super ugly, but it seemed to work, because
the worker thread would die. But when I recompiled my code for .Net 2.0,
this quit working. The call to _thread.Abort never returns, presumably
because it is blocking, waiting for the worker thread to stop blocking on
the call to the Activator.
So, any ideas how to do this? What I really need is a timeout mechanism on
the call to Activator.CreateInstance, but there isn't one. Is there another
way to approach this? I could convert the worker thread to a worker
process, and just kill the process, or do something with AppDomains, but I
was hoping for a less dramatic solution. Thoughts?
- Christopher