如何延迟关闭并在窗口服务中运行进程 [英] how to delay shutdown and run a process in window service

查看:21
本文介绍了如何延迟关闭并在窗口服务中运行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在windows关闭时运行一个进程,即一个应用程序,有什么方法可以延迟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 服务现在在独立会话中运行 并被禁止与用户互动.我在这里的回答还提供了更多细节,以及一个漂亮的图表.

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.

基本上,这是不可能的.Windows 将竭尽全力地从您的服务中启动一个单独的进程,以及您为阻止挂起的关闭所做的任何尝试.最终,用户有权覆盖您尝试拉取的任何内容.这听起来像是您应该使用安全策略而不是应用程序来解决的问题 - 在 服务器故障 上提问.

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.

这篇关于如何延迟关闭并在窗口服务中运行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆