简体   繁体   English

正确关闭应用程序:TerminateProcess的替代方法

[英]Closing an application properly: an Alternative to TerminateProcess

I'm facing an issue with TerminateProcess() function. 我面临TerminateProcess()函数的问题。 The application I'm currently writing a JobScheduler app that schedules and launches job at a specific time. 我目前正在编写该应用程序的应用程序,该应用程序可在特定时间安排和启动作业。 For this purpose, I'm using CreateProcess() to execute my JobLauncher. 为此,我使用CreateProcess()执行我的JobLauncher。

The JobLauncher process then launches a console program (using createprocess ) which effectively executes the job executable, waits for its termination and monitors the duration, user and kernel times elapsed etc. 然后,JobLauncher进程启动一个控制台程序(使用createprocess),该程序有效执行作业可执行文件,等待其终止并监视持续时间,用户和内核时间等。

In order to kill the job from the JobScheduler I firstly started using TerminateProcess() but it does not allow me to close the executable itself properly. 为了终止JobScheduler中的工作,我首先开始使用TerminateProcess(),但它不允许我正确关闭可执行文件本身。 I mean i found no way to hook any termination event. 我的意思是我找不到钩任何终止事件的方法。

Until I find a better way than a brutal TerminateProcess() , I wrote an intermediate solution using the GenerateConsoleCtrlEvent() in the calling program. 在找到比残酷的TerminateProcess()更好的方法之前,我在调用程序中使用了GenerateConsoleCtrlEvent()编写了一个中间解决方案。

In the job application that launches the target job executable, I installed a handler using SetConsoleCtrlHandler() . 在启动目标作业可执行文件的作业应用程序中,我使用SetConsoleCtrlHandler()安装了处理程序。 And in the handler, I can terminate the process of the job and notifies my thirdparties properly. 在处理程序中,我可以终止工作流程并正确通知我的第三方。 This is the better solution I found for now. 这是我现在发现的更好的解决方案。

Is there a better way to programmaticaly and properly close a process ? 有没有更好的方法以编程方式正确地关闭流程? Do you this solution is completly absurd ? 您是否完全荒谬这个解决方案? I'm not a "system-level" specialist developer though... 我不是“系统级”专家开发人员,但是...

Z. Z.

This well know Windows console problem and you can find some solutions here . 这个众所周知的Windows控制台问题,您可以在这里找到一些解决方案。

We used on internal console utility which has name "Kamikaze". 我们在内部控制台实用工具上使用了名称为“ Kamikaze”的工具。 It worked as described here and for me it's a best solution cause there is no problem with porting between Windows versions and Windows architectures (x86, x64). 它按此处描述的那样工作,对我来说,这是最佳解决方案,因为Windows版本和Windows体系结构(x86,x64)之间的移植没有问题。

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

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