0

SystemEvents.SessionEnding event is not getting fired when i shut down my system...

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • possible duplicate of [SystemEvents.SessionEnding not firing](http://stackoverflow.com/questions/7678832/systemevents-sessionending-not-firing) – Pranay Rana Apr 10 '13 at 07:21
  • Welcome to _Stackoverflow_. Your question is really low quality. What is your code? What have you tried so far? Show your work here. And more important, please read [FAQ] and [ask] – Soner Gönül Apr 10 '13 at 07:21
  • This is not a question - just a statement. One can deduce based in http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx that you probably have tried it in console application... But without code the question will be closed... – Alexei Levenkov Apr 10 '13 at 07:22
  • If you want to perform some special tasks before Closing is fired, you need to ensure that SessionEnding fires before Closing. To do this, you need to trap the WM_QUERYENDSESSION in the form by overriding the WndProc function. This example demonstrates how to do this. http://msdn.microsoft.com/en-GB/library/microsoft.win32.systemevents.sessionending.aspx – Sayse Apr 10 '13 at 07:22

2 Answers2

2

Have you tried to implement this event as the example of microsoft ? like that

Important: Console applications do not raise the SessionEnding event.

This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class. -> Message pump in .NET Windows service

private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg==WM_QUERYENDSESSION)
    {
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
        systemShutdown = true;
    }

    // If this is WM_QUERYENDSESSION, the closing event should be
    // raised in the base WndProc.
    base.WndProc(ref m);

} //WndProc 

private void Form1_Closing(
    System.Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    if (systemShutdown)
        // Reset the variable because the user might cancel the 
        // shutdown.
    {
        systemShutdown = false;
        if (DialogResult.Yes==MessageBox.Show("My application", 
            "Do you want to save your work before logging off?", 
            MessageBoxButtons.YesNo))
        {
            e.Cancel = true;
        }
        else
        {
            e.Cancel = false;
        }
    }
}

More info ? See here : SystemEvents.SessionEnding Event

Community
  • 1
  • 1
Mehdi Bugnard
  • 3,889
  • 4
  • 45
  • 86
1

You may try this - 1st you open gpedit.msc, go to Configuration -> Administrative Templates -> System -> Shutdown Options. Now choose Turn off automatic termination of applications that block or cancel shutdown.

And read Microsoft SystemEvents.SessionEnding Event documentation for further development.

  • Hi, i am not getting any Shutdown options in the path which you have mentioned... – Shyam Pramanik Apr 10 '13 at 07:38
  • Did you get in the gpedit.msc? Here is a screenshoot for your help-> https://copy.com/K7F4ySOw84EZ – Imtiaz Zaman Nishith Apr 10 '13 at 07:44
  • Hi i got gpedit.msc....but can't find Shutdown options as per the path you specified..pls help... – Shyam Pramanik Apr 10 '13 at 08:39
  • see the screen shoot given in the previous comment. – Imtiaz Zaman Nishith Apr 10 '13 at 08:47
  • Hi i already saw the screen shot..in that it is mentioned.. supported on atleast in Windows vista and i'm using windows XP..how can i find that option in windows XP... – Shyam Pramanik Apr 10 '13 at 08:55
  • Okk, then try this - Start -> Run -> regedit HKEY_CURRENT_USER\Control Panel\Desktop Make sure AutoEndTasks is 0, and set WaitToKillAppTimeout to 20000 (the default value of 2 seconds). You can set the value higher if you wish. There's also HungAppTimeout (the defalt is 5000), but that applies more for applications which are not responding. – Imtiaz Zaman Nishith Apr 10 '13 at 09:10
  • Everything you have mentioned in the registry the values are alredy there.....i don't know what to do know.. – Shyam Pramanik Apr 10 '13 at 09:22