如何从Windows服务C#取消关机 [英] How cancel shutdown from a windows service C#
问题描述
我有一个Windows服务启动(用C#编写.net2.0)。
我要当电脑关机/重启来检测和取消。
后取消我想要做一些行动,并重新启动Windows。
我已经尝试过了,但它不能正常工作
使用的Microsoft.Win32;
部分类为MyService:ServiceBase
{
保护覆盖无效的OnStart(字串[] args)
{
SystemEvents.SessionEnding + =新SessionEndingEventHandler(OnSessionEnding);
}
私人无效OnSessionEnding(对象发件人,SessionEndingEventArgs E)
{
e.Cancel = TRUE;
//做一些工作...
}
}
另一个测试:
部分类为MyService:ServiceBase
{
保护覆盖无效OnShutdown()
{
//做一些工作...
//base.OnShutdown();
}
}
的解决方法是检测关机蒙山一个winform:的 http://msdn.microsoft.com/fr-fr/library/microsoft.win32.systemevents.sessionending%28VS.80%29.aspx
但Windows杀死其他进程之前,我可以检测到它,所以它不是更好的解决方案!
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();
}
}
the solution was to detect the shutdown whith a winform : http://msdn.microsoft.com/fr-fr/library/microsoft.win32.systemevents.sessionending%28VS.80%29.aspx
but Windows kill other process before i can detect it, so it's not the better solution!
这篇关于如何从Windows服务C#取消关机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!