473,385 Members | 1,445 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,385 software developers and data experts.

c# aborting threads on exit idea

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 3246
Plater
7,872 Expert 4TB
That could work, just make sure your threads don't sit in any loops with blocking calls.
Jul 24 '08 #2
Instead of doing all this I decided to use:

Application.Exit();

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 Expert 4TB
Application.Exit() is the same call as clicking the X on the main GUI window I believe.
Jul 24 '08 #4
Application.Exit() 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.Exit() 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 Expert 512MB
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
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
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
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...
5
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: .... ...
1
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...
7
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...
2
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(...)...
6
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...
4
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...
3
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.