473,663 Members | 2,864 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.ISchedul ed comObject = null;

t = Type.GetTypeFro mProgID(ProgID) ;
activatedObject = Activator.Creat eInstance(t);
comObject = (Legacy.ISchedu led) activatedObject ;
t.InvokeMember( "Execute", BindingFlags.In vokeMethod, null,
activatedObject ,
while (Marshal.Releas eComObject(comO bject) > 0);
comObject = null;
while (Marshal.Releas eComObject(acti vatedObject) > 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.Creat eInstance never
returns. I need to kill the worker thread if it hangs on this line, but
Thread.Abort doesn't work, since the ThreadAbortExce ption isn't raised while
the thread is busy.

In .Net 1.1, I did this:

Thread t = new Thread(new ThreadStart(_th read.Abort));
t.Start();

Thread.Sleep(10 00);

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.Creat eInstance, 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 2959
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.ISchedul ed comObject = null;

t = Type.GetTypeFro mProgID(ProgID) ;
activatedObject = Activator.Creat eInstance(t);
comObject = (Legacy.ISchedu led) activatedObject ;
t.InvokeMember( "Execute", BindingFlags.In vokeMethod, null,
activatedObject ,
while (Marshal.Releas eComObject(comO bject) > 0);
comObject = null;
while (Marshal.Releas eComObject(acti vatedObject) > 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.Creat eInstance never
returns. I need to kill the worker thread if it hangs on this line, but
Thread.Abort doesn't work, since the ThreadAbortExce ption isn't raised while
the thread is busy.

In .Net 1.1, I did this:

Thread t = new Thread(new ThreadStart(_th read.Abort));
t.Start();

Thread.Sleep(10 00);

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.Creat eInstance, 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.Interrup t. But I'm not sure if the
Interrupt() will work in unmanaged code.
Jun 20 '06 #2

"Christophe r Carnahan" <cg********@ear thlink.net> wrote in message
news:eb******** ******@TK2MSFTN GP05.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.ISchedul ed comObject = null;
|
| t = Type.GetTypeFro mProgID(ProgID) ;
| activatedObject = Activator.Creat eInstance(t);
| comObject = (Legacy.ISchedu led) activatedObject ;
| t.InvokeMember( "Execute", BindingFlags.In vokeMethod, null,
| activatedObject ,
| while (Marshal.Releas eComObject(comO bject) > 0);
| comObject = null;
| while (Marshal.Releas eComObject(acti vatedObject) > 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.Creat eInstance
never
| returns. I need to kill the worker thread if it hangs on this line, but
| Thread.Abort doesn't work, since the ThreadAbortExce ption isn't raised
while
| the thread is busy.
|
| In .Net 1.1, I did this:
|
| Thread t = new Thread(new ThreadStart(_th read.Abort));
| t.Start();
|
| Thread.Sleep(10 00);
|
| 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.Creat eInstance, 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
18885
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 if there is a simple way from a main python program to kill a running thread? I see with the 'threading' module the way daemonic threads behave when the main program finishes.
5
10924
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 Accept() returns, the thread spins off another thread to handle the request, and then calls Accept() again. This all works fine except that I can find no way to kill the thread that is blocked in the Accept() call when I want to shut down the server. ...
6
42871
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 how ?? Thanks Regards
3
2844
by: Andrew Baker | last post by:
OK this has me perplexed, puzzled and bamboozled! I have a remoting service which I displayed a message box in. I then wondered what would happen if a client made a call to the service while the message box was being displayed. Since the call comes in on a different thread the client call was not blocked. This seemed to make sense to me. Next I thought, what will happen when the client calls into the server if I try to use a delegate...
13
2060
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
3496
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 activatedObject = null; Legacy.IScheduled comObject = null; t = Type.GetTypeFromProgID(ProgID);
4
20773
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 subprocess.Popen call, and afterwards, (using a web request) the python code uses the PID of the previously created process(stored in a db) and kills it with an os.kill call using the SIGKILL signal. The creation of the process is ok, apache calls the...
18
10225
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 with weirdness going on with the server I am talking to. Anyhow, this locked up state happens once in a while (maybe once per day) and I can't figure out how to deal with the locked up thread. If I issue a Thread.Abort() the exception never...
20
5068
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 threads" (see docs for threading.Thread objects). This will make the application terminate if only "daemon threads" are left. So best would probably be soemthing like
1
10379
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 CancelAsync, not cancels the call to COM. How I can do it , please ? Any suggestions will be very appreciated.
0
8858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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 we have to send another system
2
2000
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.