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

Home Posts Topics Members FAQ

c# aborting threads on exit idea

20 New Member
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 global int called WorkingThreads. I increment WorkingThreasds as the thread starts and decrement is at the end of the thread. This has worked well so far for me.


When I exit the main window I switch the KillAllThreads to true

now comes the question:

How do I make the threads listen to the killAllThreads?

here is my idea in pseudo-code:

thread executes code while killallthreads is false.
as soon as killallthreads is true my code executes the thread abort line and decrements WorkingThreads.
when WorkingThreads == 0 I exit out program.
Jul 24 '08 #1
7 3269
Plater
7,872 Recognized Expert Expert
That could work, just make sure your threads don't sit in any loops with blocking calls.
Jul 24 '08 #2
matthewaveryusa
20 New Member
Instead of doing all this I decided to use:

Application.Exi t();

I know it kills threads in waiting status (aka a window in a thread that displays passive data) quite well but will it kill threads in other states?
Jul 24 '08 #3
Plater
7,872 Recognized Expert Expert
Application.Exi t() is the same call as clicking the X on the main GUI window I believe.
Jul 24 '08 #4
matthewaveryusa
20 New Member
Application.Exi t() is the same call as clicking the X on the main GUI window I believe.
I'm a noob to c# so correct me if I'm wrong:

it isn't quite the same thing: Application.Exi t() is like clicking the X on all currently open forms (even ones open in threads) which leads me to beleive that it frees all resources allocated to the program.

Where is a good resource to see how a certain piece of code frees the memory and deals with threads?

is there some piece of code that will 100% guarantee that all ressources allocated to my app will be freed (this means process killed and threads killed)?

thanks,
-Matt
Jul 25 '08 #5
TRScheel
638 Recognized Expert Contributor
is there some piece of code that will 100% guarantee that all ressources allocated to my app will be freed (this means process killed and threads killed)?
No, you will need to clean up after yourself.

You should request each thread close before closing your application. While your killallthreads idea is nice, if you have a running list of open threads you could loop through them all and request they close without having each one of them sit in a while loop. In addition, if they are doing something like:

Expand|Select|Wrap|Line Numbers
  1. while (KillAllThreads)
  2. {
  3.       for(i = 0; i > 0; i++);
  4. }
  5.  
It wont ever hit that killallthreads. Thats a simplistic view of it, but you can easily replace that line with something that takes a long time like a file download or a lengthy equation and get much the same result. The rest of the program will end but that thread will not.

Calling the abort is a dirty way to get out of a thread too. You really want the thread to run its course and release all its resources (just because its managed code does not mean you cant code memory leaks). Allowing for an alternate path in the code to exit the thread cleanly would be ideal. How to do that depends on what the thread is doing.
Jul 25 '08 #6
matthewaveryusa
20 New Member
I let this problem aside for a while and I resolved it using exceptions.

Indeed, each thread would call a perl script that would do work. sometimes the script would stop because of connection issues. I got all my errors from the perl script and pushed them to c# to raise exceptions
voila: no killing, just catching.
As for when the user exits the app in the middle of work, I just let the thread finish on its own.

thanks,
Matt
Aug 11 '08 #7
jhaxo
57 New Member
I let this problem aside for a while and I resolved it using exceptions.

Indeed, each thread would call a perl script that would do work. sometimes the script would stop because of connection issues. I got all my errors from the perl script and pushed them to c# to raise exceptions
voila: no killing, just catching.
As for when the user exits the app in the middle of work, I just let the thread finish on its own.

thanks,
Matt
If you set the thread IsBackground to true it will be stopped when the application is killed. of course thats not very clean and if the threads are doing something with resources outside the application it will cause trouble.
Aug 11 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1448
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This framework, let us call it that, consists in two parts. The first part is just a basic thread class deriving from threading.Thread with a few extra functionality that makes it easier for a thread to spawn a new thread and "father it". Each of its
5
1803
by: Michele Simionato | last post by:
I am getting a strange error with this script: $ cat doctest-threads.py """ >>> import time, threading >>> def example(): .... thread.out = .... while thread.running: .... time.sleep(.01) .... thread.out.append(".")
1
2624
by: johnny | last post by:
In a multi-threaded application, say a worker thread makes an asynchronous call and specifies a callback method. But before the callback is executed, the thread is aborted by its creator. What is the expected behavior for this scenario? Does the thread stay alive until the callback is executed? If an exception is thrown, can it be caught? I posted this message last week, but got no response. I am really hoping someone can help me with...
7
1870
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long as 2300 ms to complete. This is fairly rare, but the test code below will definitely show it. Somehow, I must not have my design right. The idea of this code is that I do two different types of processing ( 1-starting and 2-ending) based on...
2
2046
by: Xarky | last post by:
Hi, I am writing a small program, that makes use of threads. Now in on of the threads I have a critical section, where I am using the Monitor to handle this. *** Thread_1 *** started for(...) { ....doing some work Monitor.Enter(Thread_1);
6
4997
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
4
16405
by: MSDousti | last post by:
Hi I have written a VB .NET app, which uses several threads. I thought that when the user closes the main window (when MainForm.closed event occures, and I call application.exit) all running threads must abort, but to my great surprise, running threads do not stop when application.exit is called. So I (or the users) have to stop threads using Ctrl-Alt-Del. Is there a way to stop ALL threads with a single instruction, without having to...
3
2370
by: Abubakar | last post by:
Hi, from msdn: "Closing a thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread." Through the documentation it seems that the thread proc must be exited *first* and than the CloseHandle should be called on its handle. My problem is that my threads will exit whenever they want without notifying any other part of my code. So who will take...
3
1566
by: AdrianDev | last post by:
Hi, I have a thread which I call like this: // Allocate a new thread containing class with the host getInfoThread git = new getInfoThread(host); // Create the thread and call the Go method new Thread(new ThreadStart (git.Go)).Start();
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...
1
7460
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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();...
0
5354
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...
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.