web analytics

Windows can't be shut down when a C# system tray application is running

Options

codeling 1599 - 6654
@2017-10-17 09:03:21

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?

@2017-10-17 09:04:44

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;
  }
}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com