Results 1 to 5 of 5

Thread: How Can I Tell If Windows Is Shutting Down

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    How Can I Tell If Windows Is Shutting Down

    How can i tell if windows(operating system) is being shut down?

    Thanks in advance.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: How Can I Tell If Windows Is Shutting Down

    i found this code but I have no clue where to put it
    or if it even works

    Code:
    private void SystemEvents_SessionEnded(object sender,SessionEndedEventArgs e)
    {
       bOkToExit = true;
       Application.Exit();
    }

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

    Re: How Can I Tell If Windows Is Shutting Down

    I've never seen that before myself, but I assume that it is just like any other event handler because it looks like any other event handler. From the name, that means that it's handling the SessionEnded event of the SystemEvents. I simply looked that up in the Help to find out that it's the Microsoft.Win32.SystemEvents class and the SessionEnded event is static. You therefore simply add the appropriate delegate just like you do for any other event handler:
    Code:
    public Form1()
    {
        InitializeComponent();
    
        Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
    }
    
    private void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e)
    {
        // As the documentation clearly states, you must detach the event handler or a memory leak will occur.
        // I guess this isn't too important if the system is shutting down but it's still the right thing to do.
        Microsoft.Win32.SystemEvents.SessionEnded -= new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
    
        bOkToExit = true;
        Application.Exit();
    }
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: How Can I Tell If Windows Is Shutting Down

    thanks but the code i posted up does not execute before windows shuts down.

    My program hangs around in the system tray and when the user hits the close button the program actually sets the visable property to false so the program is still running.

    The problem I run into is that windows hangs when you try to shut down the computer.
    Any ideas on how to fix this?

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

    Re: How Can I Tell If Windows Is Shutting Down

    Have you tried handling the SessionEnding event rather than SessionEnded? The most common way I've seen this done is to override the WndProc method and trap the message that is received when Windows is shutting down. I can't recall what message it is but I'm sure it's been posted before.
    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

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