473,774 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9174
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.BeginI nvoke

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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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******@devel op.com> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
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.BeginI nvoke

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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.csharp]

Nov 16 '05 #3
Hi

Process.GetCurr entProcess().Th reads 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**@discussio ns.microsoft.co m> wrote:
Process.GetCurr entProcess().Th reads 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.co m>
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.co m> wrote:
Reza <Re**@discussio ns.microsoft.co m> wrote:
Process.GetCurr entProcess().Th reads 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
Reza <Re**@discussio ns.microsoft.co m> wrote:
> Process.GetCurr entProcess().Th reads 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.co m>
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
20129
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 threading.Thread as base classes. I have tried to use call the join method of the python thread objects from C++. But although the call succeeds, the threads don't exit.
1
1440
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 display an MDI Child form within the Main Application workspace. The code bombs out when I attempt to assign the MDIParent of the child form to the Main Form (in the main thread). Some how I need to merge the spawned thread with the main thread...
2
1928
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 noticed that some threads, even after resetting the counters, have the old values.
23
7594
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 unrecoverable error occurs. When that case occurs, I create a ServiceController object and call the Stop() method. However - I get an exception thrown saying access denied. If I switch to using the LocalService account it works fine, but I lose...
5
3090
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 expected time limit, if it exceeds the limit I want to terminate the process (no mercy or graceful, just terminate the process). The spawnng of the process, counting the time and termination of process all done in C++. Some time I am not able to...
0
969
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 command that collects remote data - would this approach format work in this case? If not, what approach should I use? While Not CancelThread.WaitOne(0, False) And LoopCount < Loops ' Long data collecting task here....
1
1167
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 encounter Threads failing due to MySQL connection failures. As much as possible I plan to make the threads return the query string and let the parent script do the actual query. Thanks in advance.
1
1306
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 found to equal "Nothing". Is there some kind of trick to get the threaded subroutine to see the selectednode to process? Thank you inadvance for any assistance.
1
1785
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 new child In this case who becomes the owner of this child process. Is it the thread that spawned becomes the owner or is the main program becomes the owner of that child process. Thanks, -V
0
10106
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...
1
10046
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9915
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
8939
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...
0
6717
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
5358
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.