473,507 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to kill a thread blocked on an interop call?

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

Jun 20 '06 #1
2 3487
Christopher Carnahan wrote:
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

You could try calling Thread.Interrupt. But I'm not sure if the
Interrupt() will work in unmanaged code.
Jun 20 '06 #2

"Christopher Carnahan" <cg********@earthlink.net> wrote in message
news:eb**************@TK2MSFTNGP05.phx.gbl...
|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

A thread that currently blocks in unmanaged code cannot be aborted by
Thread.Abort, this was so in V1 and is still the same in V2, so I assume
that the thread wasn't really blocking when it "worked" on V1.
All you can do is terminate the process, or better try to fix the out-proc
server, servers that block indefinitely are broken, it's not up to the
caller process (the client) to fix it.
Willy.


Jun 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
18853
by: Jerry Sievers | last post by:
Greetings Pythonists; I have limited experience with threaded apps and plenty with old style forked heavyweight multi-processing apps. Using Python 2.3.3 on a Redhat 7.x machine. Wondering...
5
10906
by: Blatwurst | last post by:
I'm trying to implement a simple server in C#. I want to do the classic thing of spinning off a thread that just blocks in a Socket.Accept() call until a request comes in. At that point, the...
6
42855
by: RickDee | last post by:
Understand that when I start a thread, a number will be generated and is able to get from GetHashCode method. But I would like to use this number when I want to kill certain thread, anybody know...
13
2043
by: Paul | last post by:
Hi, How do I wait until a thread is finished his job then continue to the original thread? public void main(string args) { Thread t = new Thread(new ThreadStart(DoWork)); t.Start();
2
2943
by: Christopher Carnahan | last post by:
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...
4
20758
by: Richard Rossel | last post by:
Hi Fellows, I have a problem with process termination. I have a python code that apache runs through a django interface. The code is very simple, first, it creates a process with the...
18
10183
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
20
5032
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
John Dohn wrote: When I do this, I put a special value in the queue (like None) and in the worker thread, check for the special value and exit if found. Threads can also be marked as "daemon...
1
10357
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, Is it possible "kill" the thread of Backgroundworker ? In my Dowork event, I have NOT While for do e.Cancel = true, only have a call to external COM. If I want cancel, calling...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.