locked
Closing a C# App RRS feed

  • Question

  • Hi,

    Here is a question that is pretty basic but I have never dealt with this in a C# app.

    I have something I need to close when the application itself closes.

    Where/how is the best place to do this?

    Thanks

    Jeff

    Friday, August 25, 2006 2:22 PM

Answers

  • What I said above still goes, subscribe to one of those two events (FormClosing is recommendation if it is the main form of your app) so that you can do any kind of cleanup you need including closing other forms by calling their Close() method.

    If this still isn't enough, what is it you need to do when the app is closing down?

    Friday, August 25, 2006 2:47 PM

All replies

  • It depends... is this a Windows Forms application?

    If it is you can always setup an event handler for the FormClosing event and do your work there or a handler for the Application.ApplicationExit event.

    Just for note no matter where you put such a handler, it would not be triggered if your application is forcibly closed by killing it's process. When that happens the app is given no change to do anything else.

    Friday, August 25, 2006 2:27 PM
  • Hi,

    Sorry, I neglected to mention that it is a windows form app. I see in some documentation that there is a close() method, similar to VB.

    I was thinking when the user clicks X, something that I can call from there.

    Thanks

    Jeff

    Friday, August 25, 2006 2:34 PM
  • What I said above still goes, subscribe to one of those two events (FormClosing is recommendation if it is the main form of your app) so that you can do any kind of cleanup you need including closing other forms by calling their Close() method.

    If this still isn't enough, what is it you need to do when the app is closing down?

    Friday, August 25, 2006 2:47 PM
  • Hi,

    What you are saying is where I was trying to go.

    I was looking for an event named Close(), like VB. Did not exits though. Found one named FormClosing() that will do it,

    so that is going to do what I needed.

    I needed to close a com connection when the app exited.

    Thanks for the response's.

    Jeff

    Friday, August 25, 2006 3:00 PM
  • There's also the form closed event. But form closing is the better event because it would make sense to close the other form before the original one is closed.
    Friday, August 25, 2006 6:09 PM