简体   繁体   English

如何从Windows Service C#取消关闭

[英]How cancel shutdown from a windows service C#

I have a windows service started (written in C# .net2.0). 我启动了Windows服务(用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. 取消后,我想执行一些操作并重新启动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();
    }
}

I wouldn't mess with this. 我不会为此惹恼。 Windows will treat your process like a hung process (unless you go direct to the Win32 API). Windows将您的进程视为挂起的进程(除非您直接进入Win32 API)。

My suggestion would be to take note that you're being shutdown, and perhaps schedule the activities to happen on startup? 我的建议是要注意您正在关闭,并且可能安排活动在启动时进行?

UPDATE: 更新:
I think you're going to get stuck here, because your service won't know why Windows is being shutdown. 我认为您会被困在这里,因为您的服务将不知道为什么 Windows被关闭。 You'll have an infinite loop: 您将遇到无限循环:

  • Windows shutdown fires. Windows关闭触发。
  • Service notices, aborts shutdown, launches app. 服务通知,中止关机,启动应用程序。
  • User uses app to continue the shutdown. 用户使用应用程序继续关闭。
  • Window shutdown fires. 窗口关闭触发。
  • Service notices...... 服务通知...
  • Etc. 等等。

My advice would be to write your actions out to a file or the registry eg 我的建议是将您的操作写到文件或注册表中,例如

DoSomething = true
DoSomethingElse = false

Which you could read in OnStart and process. 您可以在OnStart和过程中阅读。

AbortSystemShutdown might work: AbortSystemShutdown可能起作用:

http://msdn.microsoft.com/en-us/library/aa376630%28VS.85%29.aspx http://msdn.microsoft.com/zh-CN/library/aa376630%28VS.85%29.aspx

Though I agree with Neil that it's probably not a good idea to do this. 尽管我同意Neil的观点,但这可能不是一个好主意。

Edit: Added sample code 编辑:添加了示例代码

using System.Runtime.InteropServices;

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool AbortSystemShutdown(string lpMachineName);


if (!AbortSystemShutdown("localhost"))
{
    int err = Marshal.GetLastWin32Error();
}        

You could use the shell command shutdown -a to abort the shutdown. 您可以使用shell命令shutdown -a中止关机。 I don't know if it's possible to detect a shutdown though... 我不知道是否有可能检测到关机...

Finaly I abort the idea of detect and cancel windows shutdown/reboot, But now I want detect only "shutdown -r -t xx" ! 最后,我放弃了检测并取消Windows关机/重新启动的想法,但是现在我只想检测“ shutdown -r -t xx”

Please note the operating system where is running the programme is Windows XP. 请注意,运行该程序的操作系统是Windows XP。

I have try it, but I have no ExitCode with WindowsXP: 我已经尝试过,但是在WindowsXP中没有ExitCode:

Process process = new Process();

do
{
    process.StartInfo.FileName = "shutdown.exe";
    process.StartInfo.Arguments = "/a";
    process.Start();
    process.WaitForExit();
    MessageBox.Show(string.Format("ExitCode={0}",process.ExitCode));
    Thread.Sleep(1000);
}
while (process.ExitCode != 0) ;

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 解决方案是通过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在我检测到它之前杀死了其他进程,所以这不是更好的解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM