C#, Application does not terminate | Newbie | | Join Date: May 2007
Posts: 9
| | |
Hi all,
my problem is that the application doesn't terminate when I close the form using the close box on the caption bar. Actually it seems as if the program is closed but task manager's process list shows that it is not.
There is one thread that I manully create and start in forms load event. But I set the IsBackground property of the thread to true. So, it should terminate while exiting the application.
There are other threads that refresh the grids on my form. Since I am using 2.0 framework, I set the CheckForIllegalCrossThreadCalls property of the form to false to avoid exceptions. Can the problem depend on this?
The weird thing is that I don't encounter this problem on my computer. On my computer the application terminates successfully.
|  | Moderator | | Join Date: Dec 2006
Posts: 4,745
| | | re: C#, Application does not terminate
Have you tried using the application unload event?
|  | Newbie | | Join Date: Oct 2006 Location: Mexico
Posts: 14
| | | re: C#, Application does not terminate
Try defining the thread you are creating manually as a Background worker,
when you define the thread
thread t = new thread(new threadstart(function));
t.IsBackground = true;
t.Start();
check that.. I'm not shure the property is named IsBackGround, it says something like that.. but with that property activate that thread finishes when the main thread finish.
I hopy it helps you :D
enjoy your day :D
| | Newbie | | Join Date: May 2007
Posts: 9
| | | re: C#, Application does not terminate
Thank you george, but it is exactly what I do.
It works on my computer. But a thread continues working when I test my application on another computer. There is no .NET framework on that computer. But I put the framework in setup project. May the problem depends on it ??
Thank you kenobewan. But I don't see the point in using application unload event. Can yo explain it please?
| | Newbie | | Join Date: Apr 2007
Posts: 18
| | | re: C#, Application does not terminate
if you want to force the program termination you can use Environment.Exit(0); this will terminate all the working environmnent try it may help
|  | Expert | | Join Date: Mar 2007
Posts: 202
| | | re: C#, Application does not terminate
this sounds a bit weird, try Application.Exit(); on close button event then.
|  | Newbie | | Join Date: May 2007 Location: Indiana
Posts: 2
| | | re: C#, Application does not terminate
I think what the OP is trying to say is that if you click on the 'X' using the ControlBox or use 'ALT-F4' key combination to close the main form, then the Form will directly call Application.Exit() without going through any other validation. It will not use the Form.Closed(), Form.Closing() or any other method on its way out and they are obsolete in .NET v3 anyway.
Does anybody know if the function they are calling can be overwritten?
(Also before anybody says 'Hide the ControlBox' you can still 'ALT-F4' even if it's hidden to get the same results, and setting the thread's IsBackground property doesn't work either.)
|  | Expert | | Join Date: Apr 2007 Location: Iowa
Posts: 624
| | | re: C#, Application does not terminate Quote:
Originally Posted by eflatunn Hi all,
my problem is that the application doesn't terminate when I close the form using the close box on the caption bar. Actually it seems as if the program is closed but task manager's process list shows that it is not.
There is one thread that I manully create and start in forms load event. But I set the IsBackground property of the thread to true. So, it should terminate while exiting the application.
There are other threads that refresh the grids on my form. Since I am using 2.0 framework, I set the CheckForIllegalCrossThreadCalls property of the form to false to avoid exceptions. Can the problem depend on this?
The weird thing is that I don't encounter this problem on my computer. On my computer the application terminates successfully.
Are you sure its the thread?
Try this: - Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
-
...
-
-
static void Application_ApplicationExit(object sender, EventArgs e)
-
{
-
WorkerThread.Abort();
-
}
that should force the workerthread to do a hard close, so i wouldnt suggest doing it in production, but it will show you if the problem really is the thread...
|  | Newbie | | Join Date: May 2007 Location: Indiana
Posts: 2
| | | re: C#, Application does not terminate
Yes the issue is that the threads are still running, or you want the 'X' method to use your 'Close' method that cleans stuff up. However stopping the application abruptly is a bad thing as it could have left things half done...
A bit more browsing has brought me to this: -
public const int SC_CLOSE = 0xF060;
-
public const int WM_SYSCOMMAND = 0x0112;
-
-
protected override void WndProc(ref System.Windows.Forms.Message m)
-
{
-
if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
-
MyClose();
-
// your method that cleans everything up and then runs
-
// System.Environment.Exit(0) which WILL close the threads forcefully if needed
-
-
base.WndProc(ref m);
-
}
-
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,167
| | | re: C#, Application does not terminate
I had that trouble with Threads too.
You gotta send the thread the Abort() call.
It was throw all kinds of ThreadAborting exceptions in the thread and with that you can determine inside the thread when to exit.
With a little bit of sneakiness I put it in the dispose method of the calling form usually
| | Newbie | | Join Date: Aug 2008
Posts: 1
| | | re: C#, Application does not terminate
eflatunn, did you every find the solution to this problem? I'm having it too. I set all my threads to IsBackground = true, yet sometimes on a test machine the process doesn't close even though the applications form is closed.
|  | Newbie | | Join Date: May 2009 Location: Medellin, Colombia
Posts: 2
| | | re: C#, Application does not terminate Quote:
Originally Posted by Plater I had that trouble with Threads too.
You gotta send the thread the Abort() call.
It was throw all kinds of ThreadAborting exceptions in the thread and with that you can determine inside the thread when to exit.
With a little bit of sneakiness I put it in the dispose method of the calling form usually Hi
I also have problems with my c# application which manages threads and sockets connections. I noticed that when I press close for running Application.Exit(), the program keeps on memory.
So I decided to put - Process.GetCurrentProcess().Kill();
it seemed to work pretty well. After reading your note here, I've put - Thread.CurrentThread.Abort();
It seemed to work too. But I was wondering which approach is the best for closing my application and release memory??
| | Familiar Sight | | Join Date: Dec 2008
Posts: 202
| | | re: C#, Application does not terminate
I forced the same problem with threads when I was doing database applications.
What I did is, On the thread that uses the database queries I test the connection if close the thread is aborted, and add event handler Closing or Closed to close this connection.
As the previous tone, you could declare a global field (accessed by both main thread and the other one) let's say it'll be bool workAbort is set to true Closed event handler change it to false, and your other thread should test it before any action it does.
I think now you can control many threads as you want.
Thanks,
Bassem
|  | Newbie | | Join Date: May 2009 Location: Medellin, Colombia
Posts: 2
| | | re: C#, Application does not terminate
I also use database access but with Enterprise Library 4. I think it'll be good no to worry and let this framework take care of any thread issues. That's why I didn't create any custom event handlers for closing threads. I've never needed it.
I noticed yesterday that when I use - Thread.CurrentThread.Abort();
it doesn't caused me problem and problems in debug mode, but when I execute my .exe application file directly from the bin folder, it throws an exception about cannot abort thread. The most weird thing is that this command was nested within a try cath {} block!
So I left only the statement - Process.GetCurrentProcess().Kill();
on my form_Closing event. Now I can start and close the applications, and I've got no thread issues so far.
|  | Similar .NET Framework bytes | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,562 network members.
|