Results 1 to 3 of 3

Thread: [RESOLVED] Listen for Windows Shutdown and close app problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Resolved [RESOLVED] Listen for Windows Shutdown and close app problem

    I have an application that sits in the task tray and when the user issues a shutdown request I intercept using the following code:

    I am not going to include all the DLLImport code to shorten post a little.

    Code:
    protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                if ((m.Msg == 0x11) || (m.Msg == 0x0016) || (m.Msg == 0xA123)) // WM_QUERYENDSESSION Or WM_ENDSESSION Or RF_UNLOADMSG
                {
                    // Check for Windows shutdown event
    
                    _WindowsShutdownRequested = true;
                    Application.Exit();
                }
                else
                    base.WndProc(ref m);
            }
    Then in my _FormClosing event I have the following code:

    Code:
    private void FormSettings_FormClosing(object sender, FormClosingEventArgs e)
            {
                // Check for Windows shutdown event
                if (!_WindowsShutdownRequested)
                {
                            // Code to minimize to tray goes here...
                }
            }
    This all seems to work fine, but even though my app closes and is removed from the task tray, Windows doesn't continue to shutdown. I have to click sutdown again.

    Do theses messages only listen for the shutdown event and abort it or am I doing something wrong in my code?

    Thanks in advance,
    CT

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Listen for Windows Shutdown and close app problem

    You don't need that WndProc stuff. The FormClosing event tells you why the form is closing. This code is VB.NET but you should get the idea.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Re: Listen for Windows Shutdown and close app problem

    Quote Originally Posted by jmcilhinney
    You don't need that WndProc stuff. The FormClosing event tells you why the form is closing. This code is VB.NET but you should get the idea.
    Man, I feel silly. I never even found that in any on my google searches and never even saw it as part of event (e).

    I finnaly did get the WndProc working, but I think I will give the method you provided since it is much cleaner.

    jmcilhinney, I wish I could give you more rep, since you never seem to fail in finding solutions, but you seem to be the only one ever responding.

    Thanks again,
    CT

Posting Permissions

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



Click Here to Expand Forum to Full Width