473,770 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

terminating all threads on program exit

Threads are not auto-terminated when the main form closes (not that
they should be). What's the best way to terminate them when the
program exits? Catch the on close message of the form, and give a
signal to the threads that they must shut down? Seems I need a
destructor for that. Forms don't have an overridable Dispose method.
So, how can I react when the form closes?

Note my example thread just (abnormally) runs forever until it is told
to shut down via boolean data member. So, the form's close method
could just set this. (I know there's the Abort() method, but that
seems ugly, especially since it's not guaranteed to work.)

Zytan

Mar 17 '07
10 28768
The .NET forms do have overridable Dispose methods. The reason you
can't override them is because they are already overridden. The
override is in the <Form>.Designer .cs It's safe to modify this method,
the designer won't touch it.
Right, thanks!
If you use .NET2, the preferable way to create a background worker is
the component BackgroundWorke r. It provides you the functionality of
the abovementioned class, however, it is `official'. Also, the
component uses events to communicate with the form, so for example
when you report progress, you don't have to use any Invoke()-s,
because the worker.Progress Changed event runs in the form's thread.
Seehttp://msdn2.microsoft .com/en-us/library/system.componen tmodel.backgr.. .
for details.
(Sry, I don't know anything about .NET1)
Right again! I've used this in VB, actually, and it is a very cool
way to handle worker threads. Good job, .NET. And thanks for
reminding me.
BTW: the FormClosed is your way :)
Yup, thanks, Adam! :)

Zytan

Mar 19 '07 #11

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.
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...
19
11502
by: ern | last post by:
Right now I'm using exit(0) to terminate my program. My program is a console .exe application. After the "exit(0)" line of code is encountered, the console application waits for an enter press, before terminating. I want it to terminate completely without having to press enter manually. Anybody know what I might be missing here ?
3
2146
by: Martin Arvidsson | last post by:
Hi! I am starting up a thread that is listening for tcp connections in the thread i am using while (true) { // Handle the tcp stuff!! }
2
289
by: Matthew Tylee Atkinson | last post by:
I appear to be having some problems with the isAlive() method of detecting if a thread is alive/active/running or not. I'd be grateful for any advice. I have a visualisation program (which uses PyGame Extended ) that presents content to the user and is meant to download the next batch of content whilst the current one is being displayed. To achieve this, I created a thread to perform the downloading in the background (a class based on...
0
1229
by: Lloyd Zusman | last post by:
I have a python-2.5 program running under linux in which I spawn a number of threads. The main thread does nothing while these subsidiary threads are running, and after they all complete, the main thread will then exit. I know that I can manage this through the use of Thread.join(), but when I do it as follows, the main thread doesn't respond to signals: import sys, time, signal, threading
20
541
by: greg.johnsen80 | last post by:
I have been struggling with the following problem: In my console application I have function1 calling function2 which in turn uses threads to do computationally intensive tasks. Some of the threads generate exceptions which cause the whole application to abort. How can I avoid termination and make function1 call function2 again in case of exceptions. Any ideas would be highly appreciated.
4
2258
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should start five threads and process five files at a time. Is this a good idea? I picked the number five at random... I was thinking that I might check the number of processors and start a multiple of that, but then I remembered KISS and it seemed that...
7
3269
by: matthewaveryusa | last post by:
I have been reading a lot about aborting threads and there seems to be something missing to do this easily on program exit. This is what I want to do but I will need some help. I declared a global boolean called KillAllThreads (public static bool in abstact class). I initialize it to false in my main program before my main window opens. I then have some threads open and close. I keep count of opened and closed threads by using another...
0
9454
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
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
10102
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
10038
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
9910
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
6712
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();...
1
4007
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.