Terminating Systray Application On Shutdown does not work (it does not exit the app)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Bosker

    Terminating Systray Application On Shutdown does not work (it does not exit the app)

    I have written an application that lives in the systray but when I try
    to close windows or logout it does not close my application and
    therefore prevents windows from shutting down or logging me out. It is
    essential that I solve this problem but I have run out of ideas can
    anyone help (please!)

    The application starts in Start.cs which kicks off the Systray form
    using this call:
    Application.Run (new Systray());

    Systray is a mimized form that is not visible in the TaskBar but has a
    NotifyIcon which sits in the systray. Associate with the icon is a
    context menu with a Terminate command in it i.e. Application.Exi t();
    and that works fine. Presumably windows must send some sort of
    terminate message to all running applications and I need to capture
    that? Can I use one of the standard events or do I need to use the
    windows WndProc?

    Any suggestions of where to look would be greatly appreciated.

    Regards, Jon
  • Max

    #2
    Re: Terminating Systray Application On Shutdown does not work (it does not exit the app)

    The general idea is that your application's main window should handle
    WM_QUERYSHUTDOW N and WM_SHUTDOWN correctly.

    Correctly means when you receive WM_QUERYSHUTDOW N you should answer whether
    your are ready for it or wish to cancel the shutdown because of some unsaved
    work, etc.
    When you receive WM_SHUTDOWN - you should cancel.

    Your application answers "yes" on query shutdown, but does not exit.

    I do not know exactly how event handling for those events is implemented in
    Windows.Forms. If you will not find out - you will need to overload windows
    procedure (WndProc function).

    "Jon Bosker" <GoogleJon@Visu alBasic.net> wrote in message
    news:b1303ecd.0 311042020.367ac 3ec@posting.goo gle.com...[color=blue]
    > I have written an application that lives in the systray but when I try
    > to close windows or logout it does not close my application and
    > therefore prevents windows from shutting down or logging me out. It is
    > essential that I solve this problem but I have run out of ideas can
    > anyone help (please!)
    >
    > The application starts in Start.cs which kicks off the Systray form
    > using this call:
    > Application.Run (new Systray());
    >
    > Systray is a mimized form that is not visible in the TaskBar but has a
    > NotifyIcon which sits in the systray. Associate with the icon is a
    > context menu with a Terminate command in it i.e. Application.Exi t();
    > and that works fine. Presumably windows must send some sort of
    > terminate message to all running applications and I need to capture
    > that? Can I use one of the standard events or do I need to use the
    > windows WndProc?
    >
    > Any suggestions of where to look would be greatly appreciated.
    >
    > Regards, Jon[/color]


    Comment

    • Jon Bosker

      #3
      Re: Terminating Systray Application On Shutdown does not work (it does not exit the app)

      Thanks Max - you have certainly helped me on my way. With that info I
      found some more discussion and here is what I have got:
      protected override void WndProc(ref Message m)
      {
      switch (m.Msg)
      {
      case 0x11: // WM_QUERYENDSESS ION
      TerminateApp(fa lse);
      break;

      case 0x12: // WM_QUIT
      TerminateApp(fa lse);
      break;

      case 0x16: // WM_ENDSESSION
      TerminateApp(fa lse);
      break;
      }
      base.WndProc(re f m);
      }

      My class TerminateApp then either asks the user if they want to
      terminate (if true is passed as a parameter) and if not closes up
      without question. This works in as much as the application is now
      terminated when the user tries to shut down Windows but Windows still
      does not close. The user has to tell Windows to shutdown a 2nd time
      and this time it works because my application is gone. So.... can
      anybody tell me why.... somehow I think I have to return a different
      value to Windows (I think I need to return a 1?) but I cannot see how
      to do that.

      Regards, Jon

      Comment

      • Max

        #4
        Re: Terminating Systray Application On Shutdown does not work (it does not exit the app)

        See


        It says you should return TRUE if you agree to cancel.
        To the best of my knowledge it means that you you should set m.Result=1;

        You do not need to actually close the window or application on Query End
        Session. You should just decide if you are ready to close or not and respond
        accordingly.

        You will recieve WM_ENDSESSION. WM_ENDSESSION is a proper place to close.

        See also SystemEvents.Se ssionEnding Event topic in Framework SDK
        documentation to learn about the ".NET way" to handle it.



        "Jon Bosker" <JonBosker@Yaho o.com> wrote in message
        news:2f2d948e.0 311050306.7976b 2aa@posting.goo gle.com...[color=blue]
        > Thanks Max - you have certainly helped me on my way. With that info I
        > found some more discussion and here is what I have got:
        > protected override void WndProc(ref Message m)
        > {
        > switch (m.Msg)
        > {
        > case 0x11: // WM_QUERYENDSESS ION
        > TerminateApp(fa lse);
        > break;
        >
        > case 0x12: // WM_QUIT
        > TerminateApp(fa lse);
        > break;
        >
        > case 0x16: // WM_ENDSESSION
        > TerminateApp(fa lse);
        > break;
        > }
        > base.WndProc(re f m);
        > }
        >
        > My class TerminateApp then either asks the user if they want to
        > terminate (if true is passed as a parameter) and if not closes up
        > without question. This works in as much as the application is now
        > terminated when the user tries to shut down Windows but Windows still
        > does not close. The user has to tell Windows to shutdown a 2nd time
        > and this time it works because my application is gone. So.... can
        > anybody tell me why.... somehow I think I have to return a different
        > value to Windows (I think I need to return a 1?) but I cannot see how
        > to do that.
        >
        > Regards, Jon[/color]


        Comment

        Working...