如何延时关机和运行在窗口服务流程流程、窗口

2023-09-02 01:33:53 作者:断桥烟雨ζ相思绝

我要即在窗户关闭一个应用程序运行的过程中,没有任何延迟Windows关机和运行在Windows服务中的应用方法...

I have to run a process ie a application on windows shutdown, is there any method to delay the windows shutdown and run the application in windows service...

protected override void OnShutdown()
{
    // Add your save code here
    // Add your save code here
    StreamWriter str = new StreamWriter("D:\Log.txt", true);
    str.WriteLine("Service stoped due to on" + DateTime.Now.ToString());
    str.Close();

    base.OnShutdown();
}

上面的这

我已经使用功能覆盖关机,我能写日志条目到一个文本文件,但我不能够运行后,一个应用程序在寻找,我发现,延迟低于用户只需几秒钟后,火灾停机时

I have used function above which overrides the shutdown and i was able to write a log entry to a text file but i was not able to run an application after that On searching i found that the delay was below only some seconds after user fires shutdown

this.RequestAdditionalTime(250000);

this.RequestAdditionalTime(250000);

这给了25秒关闭事件的另外的时间延迟,但我不能够运行的应用程序。任何人都可以提出方法或观念上运行关机程序。

this gives an addition time delay of 25 seconds on shutdown event but i was not able to run the application. Can anyone suggest method or ideas to run application on shutdown.

推荐答案

应用程序阻止挂起的系统关机的能力受到严格限制在Windows Vista中。详细总结在MSDN上的两个方便的文章:适用于Windows Vista 和的应用程序关闭更改在Windows Vista 。

The ability of applications to block a pending system shutdown was severely restricted in Windows Vista. The details are summarized in two handy articles on MSDN: Shutdown Changes for Windows Vista and Application Shutdown Changes in Windows Vista.

由于该页面显示,你不应该依赖于阻止停机超过5秒钟下去的能力。如果您想尝试阻止挂起的关机事件,您的应用程序应该使用新的 ShutdownBlockReasonCreate 功能,它允许你注册,说明用户为什么你认为的关机应该被阻止的原因的字符串。用户有权听取您的意见和取消关机,或扔谨慎的风和取消反正的能力。

As that page indicates, you shouldn't rely on the ability to block shutdown for any longer than 5 seconds. If you wish to attempt to block a pending shutdown event, your application should use the new ShutdownBlockReasonCreate function, which allows you to register a string that explains to the user the reason why you think the shutdown should be blocked. The user reserves the ability to heed your advice and cancel the shutdown, or throw caution to the wind and cancel anyway.

只要你的应用程序的完成的做什么,那就是不应该由一个停机中断,你应该调用相应的 ShutdownBlockReasonDestroy 功能,该释放的原因字符串,并表示该系统现在可以关闭。

As soon as your application finishes doing whatever it is that should not be interrupted by a shutdown, you should call the corresponding ShutdownBlockReasonDestroy function, which frees the reason string and indicates that the system can now be shut down.

还记得 Windows服务现在处于一个孤立的会话运行,并从被禁止与用户交互。 My回答此处还提供详情以及一个pretty的图

Also remember that Windows Services now run in an isolated session and are prohibited from interacting with the user. My answer here also provides more details, as well as a pretty diagram.

基本上,这是不可能的。窗户是要打击你的牙齿和指甲上起了一个从你的服务,以及任何试图您对阻止待定关机单独的进程。最终,用户有权否决任何试图拉动力。这听起来像你应该解决使用的安全策略,而不是一个应用程序询问,关于服务器故障的问题。

Basically, this is impossible. Windows is going to fight you tooth and nail over starting up a separate process from your Service, as well as any attempt you make to block a pending shutdown. Ultimately, the user has the power to override anything you try to pull. This sounds like something you should solve using security policies, rather than an application—ask questions about that on Server Fault.