Windows Develop Bookmark and Share   
 index > Windows Forms General > How do i close all threads when the exit button is clicked?
 

How do i close all threads when the exit button is clicked?

How do i enter code into the click event for the windows border exit button? I need to close all executing threads when that is clicked THEN exit the application.
Licht  Friday, January 25, 2008 2:42 AM

Hi,

Take a look at this post which discusses a similar issue

http://www.thescripts.com/forum/thread618181.html

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----

Suprotim Agarwal  Friday, January 25, 2008 4:33 AM

Dear Lumina,

It is a good idea to keep track of all the threads that you spwan from your main thread and then kill each of them. Or you may also use a thread pool for this purpose.Provide some more details on your requirements.

Regards,

Sandeep A

----------------------------------------------------------------------

[Mark appropriate post(s) as Answer(s)]

Sandeep Aparajit  Friday, January 25, 2008 4:30 AM

Hi,

Take a look at this post which discusses a similar issue

http://www.thescripts.com/forum/thread618181.html

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----

Suprotim Agarwal  Friday, January 25, 2008 4:33 AM
My problem is when the user clicks the exit button it only closes the main thread. This leaves the process running no matter what the user does until the thread runs out of code, i need to have that button kill off all the threads not just the main one.
Licht  Friday, January 25, 2008 4:34 AM

Hi,

Did you take a look at the link I gave you. It contains your answer.

"Threads need respect. Terminate them gracefully" Smile

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----

Suprotim Agarwal  Friday, January 25, 2008 4:56 AM
Yeah, i havn't gotten to implimentation yet though.
Licht  Sunday, January 27, 2008 10:55 PM
The recommended message thread really didn't explain things very well. Yes, if your work is UI-less, BackgroundWorker is fine, and you can set your BW to be cancellable.

However, in my case and I suspect in yours, I had a separate thread running its own form(s). I suspected that .Net Forms, being very sophisticated, knew very well how to shut its world down regardless of the number of threads, etc., created by any program. What I noticed was the the generated Program.cs didn't call Application.Exit().

So I my program's Main now looks like this:

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new FormEdit() );
Application.Exit();
}

and it appears to work fine. I know that you can do other tricks, but any type of signaling requires BeginInvoke behavior, since the form on the "other" thread won't be able to deal safely with termination while a foreign thread. Also, if you use semaphores/signals, you have to dramatically alter the message loop or set up a timer to poll the event. The .Net people certainly solved this problem-- reliability demands it.
davidhov  Sunday, February 10, 2008 10:44 PM
The above post's answer (be me, also) is OK, but the drawback is that the forms closed during Application.Exit() are somewhat brutally killed. In my case, that was fine.

A simple but more polite way to do it is as follows. Assume that one form has spawned another, but the second is on a separate thread. When the "parent" or original thread starts to close, it can close the other form, but it must do so correctly. It can't just call frmOther.Close(), because almost all form members require they be called on the form's main thread.

private FormTestThreading frmtt = null;

public delegate void SimpleCall ();

private void FormXlate_FormClosing ( object sender, FormClosingEventArgs e )
{
if ( frmtt != null )
{
SimpleCall sc = new SimpleCall( frmtt.Close );
frmtt.BeginInvoke( sc );
frmtt = null;
}
}


The code above creates a delegate and performs a safe, cross-thread call to the other form's Close() method. Ignoring the returned IAsyncWhatever appears to be safe according to the documentation I've read.

Generally, this will cause the form-running separate thread to exit its message loop and return back to the thread's start function. That exits, causing the thread itself to die.
davidhov  Sunday, February 10, 2008 11:17 PM

You can use google to search for other answers

Custom Search

More Threads

• Sorting listbox by second column
• infinite recursion iniatializing user control
• Splitter and SplitContainer
• Adding User Entered Text in a Combo Box
• Problem with DataGridView and CancelButton property
• WebBrowser control stops all events from firing.
• Dockable windows with pin and autohide feature
• simple question
• Opening multiple children
• remote desktop connection