detect shutdown in window service

微笑、不失礼 提交于 2019-11-30 08:39:40

For a shutdown, override the OnShutdown method:

protected override void OnShutdown()
{
    //your code here
    base.OnShutdown();
}

For a logoff:

First, add an event handler to Microsoft.Win32.SystemEvents.SessionEnded in the Service Constructor:

public MyService()
{
    InitializeComponent;
    Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
}

Then add the handler:

void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e)
{
    //your code here
}

This should catch any ended session, including the console itself (the one running the services).

Timoteo

I know I'm bringing this up from the dead but I found it helpful and hope to add a little to the topic. I'm implementing a WCF duplex library hosted in a Windows Service and came across this thread because I needed to detect, from within the windows service, when a user logs off or shuts down the computer. I'm using .Net Framework 4.6.1 on Windows 7 and Windows 10. Like previously suggested for shutdown what worked for me was overriding ServiceBase.OnShutdown() like so:

protected override void OnShutdown()
{
    //Your code here

    //Don't forget to call ServiceBase OnShutdown()
    base.OnShutdown();
}

Remember to add the following to your service's constructor to allow the shutdown event to be caught:

CanShutdown = true;

Then to capture when a user logs off, locks the screen, switches user, etc. you can just override the OnSessionChange method like so:

protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
    if (changeDescription.Reason == SessionChangeReason.SessionLogoff)
    {
        //Your code here...

        //I called a static method in my WCF inbound interface class to do stuff...
    }

    //Don't forget to call ServiceBase OnSessionChange()
    base.OnSessionChange(changeDescription);
}

And of course remember to add the following to your service's constructor to allow catching of session change events:

CanHandleSessionChangeEvent = true;

You should override OnShutdown in your service

// When system shuts down
protected override void OnShutdown()
{
    // Add your save code here
    base.OnShutdown();
}

You might also want to override OnStop

// When the user presses stops your service from the control panel
protected override void OnStop()
{
    // Add your save code here too
    base.OnStop();
}

Edit:
If you really want to listen to the shutdown event Microsoft.Win32.SystemEvents.SessionEnding is the way to go.

Maybe you can use this. Poll the method in question every now and then (1 second interval) and you'll be able to do what you want.

You need RegisterServiceCtrlHandlerEx API call.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!