force exit Form

This is a discussion on force exit Form within the C# Programming forums, part of the General Programming Boards category; How can you force exit an application? Like I have some while/for loops that don't exit when I press the ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    force exit Form

    How can you force exit an application? Like I have some while/for loops that don't exit when I press the X.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Are they running in another thread? If they are long loops on the same thread of the message pump then your close messages wont get through. So maybe seperate those long intensive tasks on another thread and then insure those threads are ended upon close.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, technically no they are not running on the same thread. Meaning that if you press the X button on a Form it will close. The main program though, the myProgram.exe process, won't terminate because in some event handler there is a while/for loop that has to be terminated via user input. I don't know exactly how events are handled, so maybe one main thread that handles signals? In any case the signal passes to the program.
    So I could add an extra variable in every while loop, lets say "kiiiiilmee", like:
    Code:
    while(var || kiiiiilmee)
    {...}
    and add something to Application.AplicationExit delegate. But I just wanted something that does this for me automatically. Something that will translate to "kill the process with all active threads".

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    If you are raising your events with begininvoke() they are running on a seperate thread. In your handler though on your forms thread you can marshal it back into your UI's thread with this.invoke(). That way assuming your windows messages get processed, everything will end.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    never use Invoke() for UI, always use BeginInvoke(). using the synchronous version is a very easy way to get your application to deadlock.

    anyways, back on topic, Application.Exit() should work. fail that, System.Diagnostics.Process.Kill()

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Can you elaborate more?
    I have a Main that executes Application.Run(new Form1()); Then it is just event handlers, nothing special. I don't manually create any new thread.
    So I guess there is only one thread running. So why wouldn't an exit method work on this one thread and force it to exit? I don't use invoke and I am not really familiar with what it does. Kind of new to this stuff so I don't know everything (yet).

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Ok. The kill() methods works if I make a seperate button and call it. If not then it doesn't work. So I made a button. Can I somehow associated it with already-made X button?

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    The way that Windows works is there is only 1 GUI thread. Everything is drawn to a screen by 1 single thread. (Begin)Invoke allows you to execute code on the GUI thread. A common 'design pattern' is like this:

    Code:
    private void UpdateSomeControl(object o, EventArgs e) {
      if (this.InvokeRequired) {
        this.BeginInvoke((EventHandler)UpdateSomeControl, o, e);
      } else {
        this.Text = "blah";
      }
    }
    if you don't do that you'll get a cross-thread exception and it'll crash your application, or u could catch the exception and then your controls don't get updated.

    basically, try to use the GUI thread only to do GUI stuff. since you mentioned you have some while/loops that suggests that that stuff should not be done on the GUI thread, and you should create your own thread or use ThreadPool and then invoke the GUI controls when you need to update.

    anyways, there's a FormClosing event you can listen to which will fire when you hit the 'X'.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Thanx
    I 'll use FormClosed and Process.Kill() and I keep in mind that there is a better way doing so.

    By the way, is there a way to disable the X button and just make my custom one? Or disable it?

  10. #10
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You could set your forms border style to none and draw your own I suppose. Theres probably a better solution for that though. I know its very easy to do with WPF though.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Actually for my purpose that is what I should do, since it is a game and not an actual GUI. I ll search msdn how to do this stuff.

    Also, the kill() doesn't always work. Sometimes it still freezes. I suppose if I put it on the FormClosing and not the FormClosed it would work.

    So, if I skip the X button and make my own quit buttot, what is the best way to exit? Is it safe to try Application.Exit(), if that doesn't work then Process.Kill(). Is this way "unsafe" for whatever reason??

  12. #12
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    http://msdn.microsoft.com/en-us/libr...ll(VS.71).aspx

    Whats wrong with just jusing Form.Close() and letting it return cleanly and automatically.

  13. #13
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    i'm guessing there's some intensive work being done which is preventing the GUI thread from closing.

  14. #14
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Then that intensive work should probably be on another thread in the background.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  2. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  3. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 08:27 AM
  4. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM
  5. Making an MFC form to suit
    By TJJ in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2004, 11:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21