Detect Shutdown from .NET

  • Thread starter Thread starter Adnan Hebibovic
  • Start date Start date
A

Adnan Hebibovic

Hello developers

How can I detect Windows shutdown from .NET and I mean clean .NET not
catching the events with Win32 API ...

Thanks
 
When the form is minimized to systray there is no Win32.SessionEnded event
....

Thanks
 
Your first post asked how to "detect Windows shutdown". My response answered
that. I just tried it out, and it worked (although I used session ending
vice session ended). I run my program, I try to shut down windows, and the
event fires. Your second post asks about minimizing. For that, you should
handle the form's resize event and check the form property WindowState. If
it is FormWindowState.Minimized, then the window is minimized.

Windows shutdown and minimizing a window are different things. What exactly
are you trying to detect?
 
Hi,

I think Adnan might be asking that when he minimized a window to the system
tray, the SessionEnded event is not fired. Right?

I have tried it on my machine, and it works fine under any circumstances.
Please try my code in a new windows application.

private void Form1_Load(object sender, System.EventArgs e)
{
Microsoft.Win32.SystemEvents.SessionEnded +=new
Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
this.Resize +=new EventHandler(Form1_Resize);
}

private void SystemEvents_SessionEnded(object sender,
Microsoft.Win32.SessionEndedEventArgs e)
{
MessageBox.Show("End");
}

private void Form1_Resize(object sender, EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.notifyIcon1.Visible=true;
}
else
this.ShowInTaskbar = true;
this.notifyIcon1.DoubleClick+=new EventHandler(notifyIcon1_DoubleClick);
}

private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
You're welcome, Adnan.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top