473,569 Members | 2,438 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 3490
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
18865
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...
5
10916
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...
6
42865
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
13
2049
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
2953
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
20762
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...
18
10209
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...
20
5046
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...
1
10369
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
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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. ...
0
8138
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...
1
7679
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
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...
0
3657
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
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
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.