473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Terminating Spawned Threads

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
Nov 16 '05 #1
6 9131
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/<#N**************@TK2MSFTNGP10.phx.gbl>

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]
Nov 16 '05 #2
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

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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/<#N**************@TK2MSFTNGP10.phx.gbl>

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]

Nov 16 '05 #3
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.
"news.microsoft.com" wrote:
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

Nov 16 '05 #4
Reza <Re**@discussions.microsoft.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Reza <Re**@discussions.microsoft.com> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
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.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Reza <Re**@discussions.microsoft.com> wrote:
> 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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

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

Similar topics

3
by: DE | last post by:
Hello, I have an app with embedded Python. Python scripts create their own threads and I need to terminate these threads at the point where the user wants to leave the application. I use...
1
by: Neds | last post by:
I'm having a major problem with threads. I have a main application that dynamically loads plugins. One of the plugins receives SMS messages in a seperate thread. When an SMS comes in I need to...
2
by: Naveen Mukkelli | last post by:
Hi, I'm working on a multi-threaded application. I'm using ThreadPools. One of the requirements of the application is, if certain exception occurs all the counters have to be reset. I have...
23
by: Adam Clauss | last post by:
I have a C# Windows Service running as the NetworkService account because it needs to access a network share. As part of the service's initialization, I want the service to terminate, if an...
5
by: GTS | last post by:
Hi All, I am spawning a process from a service. The spawned process hungs for various reasons, (corrupted data, deadlock). I am expecting the process has to complete the task with in the...
0
by: JJ | last post by:
I need to allow the user to cancel a thread. The examples I've seen seem to set a 'cancel requested' flag in a while..do loop in the threads code. However, my threads task is a mainly a single...
1
by: Alvin A. Delagon | last post by:
Is there any way to fetch the Return results of spawned threads within the parent script? I would like to do that because I'm having problems with Threads that do queries to a database, I often...
1
by: Dean Hinson | last post by:
Hello, I am trying to spawn a sunroutine in a worker thread for populating a trreeview. I set the selectednode and then start the thread. However, the thread errors because the selectednode was...
1
by: Vishal Sethia | last post by:
Just trying to understand the behaviour of spawn. Consider I have a function which creates two threads. And in one of the threads I make a call to pexpect.spawn. spawn would fork and create a new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.