How cancel shutdown from a windows service C#

Posted by scrat789 on Stack Overflow See other posts from Stack Overflow or by scrat789
Published on 2010-04-27T09:43:14Z Indexed on 2010/04/27 9:53 UTC
Read the original article Hit count: 521

Hello everybody

I have a windows service started (written in C# .net2.0).

I want to detect when the computer shutdown/reboot and cancel it. After the cancel I want do some actions and restart windows.

I have tried it, but it not working

using Microsoft.Win32;
partial class MyService: ServiceBase
{
    protected override void OnStart(string[] args)
    {
        SystemEvents.SessionEnding += new SessionEndingEventHandler(OnSessionEnding);
    }

    private void OnSessionEnding(object sender, SessionEndingEventArgs e)
    {
        e.Cancel = true;
        //Do some work...
    }
}

Another test:

partial class MyService: ServiceBase
{
    protected override void OnShutdown()
    {
        //Do some work...
        //base.OnShutdown();
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about windows-services