dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] Detect application quit

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Resolved [RESOLVED] Detect application quit

    Hello. I am wondering if there are any ways to detect when the user is attempting to exit the program by means that you haven't specifically coded for.

    Obviously a power outage would fall into the category of non-detectable exits but maybe things like right-clicking the icon in the start menu, and perhaps even ctrl+alt+delete would be detectable.

    The reason I want to detect as many of these as possible is because I send a logout message to the server when the user quits in order to save server from the trouble of having to errorhandle the connection loss.

    Thanks

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,918

    Re: Detect application quit

    Sure. Add a Boolean property to My.Settings. Set it to True when the application starts up, and set it to False when the application shuts down by a means that you are expecting. If the application starts and finds the property already True, then the application shut down in a way that you didn't expect.
    My usual boring signature: Nothing

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Detect application quit

    You have not indicated what events you are currently handling, so here are two basic ones you can use.

    1. Application.ApplicationExit.
    2. Microsoft.Win32.SystemEvents.SessionEnding. Pay heed to the remarks section about monitoring for WM_QUERYENDSESSION.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Detect application quit

    Quote Originally Posted by Shaggy Hiker View Post
    Sure. Add a Boolean property to My.Settings. Set it to True when the application starts up, and set it to False when the application shuts down by a means that you are expecting. If the application starts and finds the property already True, then the application shut down in a way that you didn't expect.
    Yes, but that would only detect it the next time I start the application, not before I quit (which is what the server needs to know at that time).

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Detect application quit

    What I do now is

    Code:
    VbKeyEscape
       SendToServer(QUIT)
       Quit application
    I want to do that for as many application exits as possible. For instance, windows promts me of unsaved work if I opt to right-click -> quit on a notepad file, or even when I attempt to shut down my computer.
    Last edited by Nirwanda; Dec 31st, 2016 at 03:13 PM.

  6. #6
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,347

    Re: Detect application quit

    Quote Originally Posted by Nirwanda View Post
    What I do now is

    Code:
    VbKeyEscape
       SendToServer(QUIT)
       Quit application
    I want to do that for as many application exits as possible. For instance, windows promts me of unsaved work if I opt to right-click -> quit on a notepad file, or even when I attempt to shut down my computer.
    Notepad doesn't need to determine how the form is closing just that it is ... to do this just handle the FormClosing Event.

    ... If you have a single form you can do something similar.

    Kris

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Detect application quit

    Thank you TmTin and ioo, that got me started.

    Experimenting with
    Code:
    AddHandler Application.ApplicationExit, AddressOf OnApplicationExit
    ... at the moment.


    I was sort of barking up the wrong tree though since I am using XNA primarily. The winform close event was obviously not detected when I quit the XNA part of my application, however there was an equivalent for it in the xna game class:

    Code:
    Protected Overrides Sub OnExiting(ByVal sender As Object, ByVal args As EventArgs)
    I am using both these codes now and it's working great, even detecting ctrl+alt+del force quits etc.
    Last edited by Nirwanda; Jan 1st, 2017 at 12:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width