472,145 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

C#, Application does not terminate

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.
May 7 '07 #1
16 47828
kenobewan
4,871 Expert 4TB
Have you tried using the application unload event?
May 7 '07 #2
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
May 7 '07 #3
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?
May 8 '07 #4
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
May 8 '07 #5
vanc
211 Expert 100+
this sounds a bit weird, try Application.Exit(); on close button event then.
May 9 '07 #6
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.)
May 30 '07 #7
TRScheel
638 Expert 512MB
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:


Expand|Select|Wrap|Line Numbers
  1. Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
  2.         ...
  3.  
  4.         static void Application_ApplicationExit(object sender, EventArgs e)
  5.         {
  6.                WorkerThread.Abort();
  7.         }
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...
May 30 '07 #8
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:
Expand|Select|Wrap|Line Numbers
  1.         public const int SC_CLOSE = 0xF060;
  2.         public const int WM_SYSCOMMAND = 0x0112;
  3.  
  4.         protected override void WndProc(ref System.Windows.Forms.Message m)
  5.         {
  6.             if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
  7.                 MyClose();  
  8. // your method that cleans everything up and then runs
  9. // System.Environment.Exit(0) which WILL close the threads forcefully if needed
  10.  
  11.             base.WndProc(ref m);
  12.         }
  13.  
May 30 '07 #9
Plater
7,872 Expert 4TB
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
May 30 '07 #10
Gremsy
1
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.
Aug 5 '08 #11
@Plater
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

Expand|Select|Wrap|Line Numbers
  1. Process.GetCurrentProcess().Kill();
it seemed to work pretty well. After reading your note here, I've put

Expand|Select|Wrap|Line Numbers
  1. Thread.CurrentThread.Abort();
It seemed to work too. But I was wondering which approach is the best for closing my application and release memory??
May 16 '09 #12
Bassem
344 100+
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
May 16 '09 #13
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

Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1. 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.
May 16 '09 #14
@george1106
Thanks george1106

I tried your advice about the

thread t = new thread(new threadstart(function));
t.IsBackground = true;
t.Start();

"IsBackground" is the right property name and it worked out great. As soon as i terminate the program, the thread terminates and disappears from the task manager.
Apr 26 '10 #15
Plater
7,872 Expert 4TB
Huh, how about that, learn something new everyday.
A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.
Apr 26 '10 #16
I have experienced tis too, and this is what i did

private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{


Application.Exit();



}
I hope this works, cos it wrked for me
Jul 20 '10 #17

Post your reply

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

Similar topics

3 posts views Thread by Matthias Jentsch | last post: by
6 posts views Thread by Max | last post: by
reply views Thread by leo001 | last post: by

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.