Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. How to detect when an application is about to exit

How to detect when an application is about to exit

Scheduled Pinned Locked Moved .NET (Core and Framework)
agentic-aihelptutorial
6 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodeBerserker
    wrote on last edited by
    #1

    Hello. In my windows application I have a subclassed Form (MyWin) that is opened as a top level window with myWin.Show(). MyWin contains a RichTextBox where the user is supposed to type in text. When myWin is closed by the user the text is saved to a file. I implement this by overriding OnClosing in MyWin and save the text from the RichTextBox there. This works fine when the user explicitly close myWin. But since myWin isn’t the main window in the application myWin can be closed implicitly when the application exits. When this happends the overridden OnClosing function will not be called, so the text will not be saved in this case. I have tried to add a delegate to Application.ApplicationExit and Application.ThreadExit from MyWin to save the text from there. But that doesn’t work because when these delegates are called the RichTextBox has already been destroyed and its Text field is empty. I want MyWin to be autonomous so I don’t want to be dependent on some Save function that must be called when the main window closes. So, what I need is someway to detect when the application is about to close, something like Application.BeforeExit but I cannot find anyway to do this. I thought that if I could retrieve the main window in the application,from MyWin, I could add a delegate to its Closing event, but I cannot find any way to do this. I have searched and found Application.Current.MainWindow in PresentationFramework, which I don’t know what it is, but when I try to use this the Application.Current is always null. So, if anyone knows how to detect when the application is about to be closed before anything is actually closed I would appreciate some help. Regards Peter

    L V J Q 4 Replies Last reply
    0
    • C CodeBerserker

      Hello. In my windows application I have a subclassed Form (MyWin) that is opened as a top level window with myWin.Show(). MyWin contains a RichTextBox where the user is supposed to type in text. When myWin is closed by the user the text is saved to a file. I implement this by overriding OnClosing in MyWin and save the text from the RichTextBox there. This works fine when the user explicitly close myWin. But since myWin isn’t the main window in the application myWin can be closed implicitly when the application exits. When this happends the overridden OnClosing function will not be called, so the text will not be saved in this case. I have tried to add a delegate to Application.ApplicationExit and Application.ThreadExit from MyWin to save the text from there. But that doesn’t work because when these delegates are called the RichTextBox has already been destroyed and its Text field is empty. I want MyWin to be autonomous so I don’t want to be dependent on some Save function that must be called when the main window closes. So, what I need is someway to detect when the application is about to close, something like Application.BeforeExit but I cannot find anyway to do this. I thought that if I could retrieve the main window in the application,from MyWin, I could add a delegate to its Closing event, but I cannot find any way to do this. I have searched and found Application.Current.MainWindow in PresentationFramework, which I don’t know what it is, but when I try to use this the Application.Current is always null. So, if anyone knows how to detect when the application is about to be closed before anything is actually closed I would appreciate some help. Regards Peter

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, [EDIT}Use the FormClosing event, not the Closing event![/EDIT] if the MainForm opened some OtherForm which may hold dirty data, then MainForm.OnFormClosing should perform an OtherForm.Close(), and of course OtherForm.OnFormClosing should deal with the dirty data. BTW: If keeping track of which forms got opened is too difficult, you could rely on Application.OpenForms and try and close those Forms one by one. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      modified on Sunday, August 23, 2009 4:24 AM

      1 Reply Last reply
      0
      • C CodeBerserker

        Hello. In my windows application I have a subclassed Form (MyWin) that is opened as a top level window with myWin.Show(). MyWin contains a RichTextBox where the user is supposed to type in text. When myWin is closed by the user the text is saved to a file. I implement this by overriding OnClosing in MyWin and save the text from the RichTextBox there. This works fine when the user explicitly close myWin. But since myWin isn’t the main window in the application myWin can be closed implicitly when the application exits. When this happends the overridden OnClosing function will not be called, so the text will not be saved in this case. I have tried to add a delegate to Application.ApplicationExit and Application.ThreadExit from MyWin to save the text from there. But that doesn’t work because when these delegates are called the RichTextBox has already been destroyed and its Text field is empty. I want MyWin to be autonomous so I don’t want to be dependent on some Save function that must be called when the main window closes. So, what I need is someway to detect when the application is about to close, something like Application.BeforeExit but I cannot find anyway to do this. I thought that if I could retrieve the main window in the application,from MyWin, I could add a delegate to its Closing event, but I cannot find any way to do this. I have searched and found Application.Current.MainWindow in PresentationFramework, which I don’t know what it is, but when I try to use this the Application.Current is always null. So, if anyone knows how to detect when the application is about to be closed before anything is actually closed I would appreciate some help. Regards Peter

        V Offline
        V Offline
        VikashGohil
        wrote on last edited by
        #3

        See you can add new Class and a Method in that Class. Then you can add a Handler for Application.Exit event which references the above Class method. See below Code. Class File Code: Public Class ShutDown       Public Shared Sub OnShutdown(ByVal sender As Object, ByVal e As System.EventArgs)             Try             Catch ex As Exception             Finally             End Try       End Sub End Class Form Code That Loads When Application Starts: Sub New()             InitialiseComponents             AddHandler Application.ApplicationExit, AddressOf ShutDown.OnShutdown End Sub The Above Code would Catch the Application.Exit event and run the OnShutDown Procedure. You can write the Code that you want to Execute in the OnShutDown Method. Hope This Helps.

        1 Reply Last reply
        0
        • C CodeBerserker

          Hello. In my windows application I have a subclassed Form (MyWin) that is opened as a top level window with myWin.Show(). MyWin contains a RichTextBox where the user is supposed to type in text. When myWin is closed by the user the text is saved to a file. I implement this by overriding OnClosing in MyWin and save the text from the RichTextBox there. This works fine when the user explicitly close myWin. But since myWin isn’t the main window in the application myWin can be closed implicitly when the application exits. When this happends the overridden OnClosing function will not be called, so the text will not be saved in this case. I have tried to add a delegate to Application.ApplicationExit and Application.ThreadExit from MyWin to save the text from there. But that doesn’t work because when these delegates are called the RichTextBox has already been destroyed and its Text field is empty. I want MyWin to be autonomous so I don’t want to be dependent on some Save function that must be called when the main window closes. So, what I need is someway to detect when the application is about to close, something like Application.BeforeExit but I cannot find anyway to do this. I thought that if I could retrieve the main window in the application,from MyWin, I could add a delegate to its Closing event, but I cannot find any way to do this. I have searched and found Application.Current.MainWindow in PresentationFramework, which I don’t know what it is, but when I try to use this the Application.Current is always null. So, if anyone knows how to detect when the application is about to be closed before anything is actually closed I would appreciate some help. Regards Peter

          J Offline
          J Offline
          Jack Vanderhorst
          wrote on last edited by
          #4

          I think you may be looking for a specific Windows message... You can override PreProcessMessage in your form and basically "hook" all manner of unusual things. I wish I knew which one you were looking for exactly or if it even exists, but whenever I've had to handle unusual user actions that didn't really exist as ordinary events, this has worked for me. It's just been a matter of finding the correct combination of msg.Msg, msg.HWnd, and msg.LParam that you are looking for in the overriden PreProcessMessage routine.

          1 Reply Last reply
          0
          • C CodeBerserker

            Hello. In my windows application I have a subclassed Form (MyWin) that is opened as a top level window with myWin.Show(). MyWin contains a RichTextBox where the user is supposed to type in text. When myWin is closed by the user the text is saved to a file. I implement this by overriding OnClosing in MyWin and save the text from the RichTextBox there. This works fine when the user explicitly close myWin. But since myWin isn’t the main window in the application myWin can be closed implicitly when the application exits. When this happends the overridden OnClosing function will not be called, so the text will not be saved in this case. I have tried to add a delegate to Application.ApplicationExit and Application.ThreadExit from MyWin to save the text from there. But that doesn’t work because when these delegates are called the RichTextBox has already been destroyed and its Text field is empty. I want MyWin to be autonomous so I don’t want to be dependent on some Save function that must be called when the main window closes. So, what I need is someway to detect when the application is about to close, something like Application.BeforeExit but I cannot find anyway to do this. I thought that if I could retrieve the main window in the application,from MyWin, I could add a delegate to its Closing event, but I cannot find any way to do this. I have searched and found Application.Current.MainWindow in PresentationFramework, which I don’t know what it is, but when I try to use this the Application.Current is always null. So, if anyone knows how to detect when the application is about to be closed before anything is actually closed I would appreciate some help. Regards Peter

            Q Offline
            Q Offline
            qmartens
            wrote on last edited by
            #5

            Override OnClosed in MyWin instead of OnClosing. The RichTextBox.Text property will still be valid. MyWin.OnClosed does get called when the main form is closed, whereas MyWin.OnClosing does not.

            Eagles my fly, but weasels don't get sucked into jet engines.

            L 1 Reply Last reply
            0
            • Q qmartens

              Override OnClosed in MyWin instead of OnClosing. The RichTextBox.Text property will still be valid. MyWin.OnClosed does get called when the main form is closed, whereas MyWin.OnClosing does not.

              Eagles my fly, but weasels don't get sucked into jet engines.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              The Closed or FormClosed events are too late to interact with the user. Use the FormClosing event, not the Closing event! :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups