Terminating Spawned Threads

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

I have a very multi-threaded Windows Forms application. There are many
BeginInvoke calls, as well as Thread/ThreadStarts.

My issue is that when I quit my application there are often threads hanging
around that prevent the process from dying.

I've gone through my code line by line looking for orphaned threads, but I'm
unable to figure out where they are being created.

Is there any way to kill all threads spawned by a process without having a
reference to those threads handy?

If not, is there anyway in VS.NET to easily look for these threads? I know
there is the Threads window, but do I have to set a breakpoint in every
possible piece of code to get it to show up in the threads window?

Any tips would be appreciated.

RMD
 
If you just want the threads to end when the application ends you have tewo options depending on the type of work happening:

if you are doing lots of short tasks that don't require lots of control over the threads, use the thread pool via Delegate.BeginInvoke

if you are doing long running tasks, polling, blocking, etc, set the IsBackground poerty of the threads to true.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#[email protected]>

I have a very multi-threaded Windows Forms application. There are many
BeginInvoke calls, as well as Thread/ThreadStarts.

My issue is that when I quit my application there are often threads hanging
around that prevent the process from dying.

I've gone through my code line by line looking for orphaned threads, but I'm
unable to figure out where they are being created.

Is there any way to kill all threads spawned by a process without having a
reference to those threads handy?

If not, is there anyway in VS.NET to easily look for these threads? I know
there is the Threads window, but do I have to set a breakpoint in every
possible piece of code to get it to show up in the threads window?

Any tips would be appreciated.

RMD



[microsoft.public.dotnet.languages.csharp]
 
I think I've narrowed things down to the Updater Application Block. It seems
to spawn a thread (or more than one) internally and then is not a good boy
about cleaning things up. This thread appears to be a foreground thread,
which is why my application's EXE remains open even after the form is
closed.

Looks like I'll have to start hunting through the UAB code to find the
culprit... sigh...

Thanks,
RMD
 
Hi

Process.GetCurrentProcess().Threads gets all the threads of you
application and you can check the IsAlive property to check if it is still
running and if so killl it.
 
Reza said:
Process.GetCurrentProcess().Threads gets all the threads of you
application and you can check the IsAlive property to check if it is still
running and if so killl it.

Note that you can't use the IsAlive property on a ProcessThread - you
can use ThreadState though. I don't believe you can kill threads in
other processes, either.
 
Jon Skeet said:
Note that you can't use the IsAlive property on a ProcessThread - you
can use ThreadState though. I don't believe you can kill threads in
other processes, either.

Oops - what I meant by the last bit is that I can't see how you'd kill
a ProcessThread as opposed to a Thread - whether or not it's in the
same process.
 
Actually, with interop you can kill a process thread very easily, but I
advise against it....as an experiment I did that way back in the early days,
and it sure does kill it. The problem is that the CLR does not know that
you've just done something very nasty with one of the process threads that
it had mapped to a managed thread. It locks up that thread beyond hope of
recovery.
 
Back
Top