Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tray application allow system to kill my program when logouting or exiting

My program is starting with windows, I have such code to prevent exiting application when user hits exit button:

    protected override void OnClosing(CancelEventArgs e)
    {
        this.ShowInTaskbar = false;
        e.Cancel = true;
        this.Hide();
    }

It is working very well, but when I want to turn off computer, every time I get screen like 'One or more application cannot be exited. Cancel - Exit despite this.

How can I allow windows to normally exit my application, but also prevent user from exiting it when click red exit button?

like image 472
Qentinios Avatar asked Nov 18 '25 03:11

Qentinios


1 Answers

See How to detect Windows shutdown or logoff.

In the SessionEnded event handler, set a boolean like sessionEnded and in your OnClosing test for that value:

if (!sessionEnded)
{
    e.Cancel = true;
    this.Hide();
}
like image 106
CodeCaster Avatar answered Nov 20 '25 16:11

CodeCaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!