web analytics
Windows can't be shut down when a C# system tray application is running Options
codeling
Posted: Tuesday, October 17, 2017 9:03:21 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 919
Points: 3861

I have a C# sytem tray application which is minimized in the system tray when the user closes the form by using the following code:

private void TrayForm_FormClosing(object sender, FormClosingEventArgs e)
{
  Hide();
  e.Cancel = true;
}

It works great except the computer does not shut down because the system tray application keep hidding instead of closing itself.

Is there a way to close the form in case when the Windows is shutting down?

Sponsor
Posted: Tuesday, October 17, 2017 9:03:21 AM
 
codeling
Posted: Tuesday, October 17, 2017 9:04:44 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 919
Points: 3861

In .NET Framework, the CloseReason enumeration can be used to identify the reason that a form was closed, so I can trap the reason the form is closing and actually close it if the Windows is shutting down...

private void TrayForm_FormClosing(object sender, FormClosingEventArgs e)
{
  if (e.CloseReason != CloseReason.WindowsShutDown)
  {
    Hide();
    e.Cancel = true;
  }
}
Users browsing this topic
Guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2017 Digcode.com. All rights reserved.