C ++进程终止 [英] C++ Process Terminating

查看:138
本文介绍了C ++进程终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

因此,在我的程序中,我使用EnumProcess获取所有活动进程,然后将此ID与文件进行比较,如果我找到相同的ID,则终止它,

我现在的问题是如何终止程序,我应该使用TerminateProcess吗?有没有更简单,更安全的方法?和ty

解决方案

当然,你可以使用 TerminateProcess ,这甚至不是那么危险(相比之下) TerminateThread ),但这只是在极端情况下使用的肮脏事物;例如,OS可以终止错误的进程。在常规使用中,进程应该自行终止,只需从其入口点函数返回即可。它应该以这种方式工作,即使你决定终止一些致命异常的进程,但即使这很少见,并且主要用于最简单的仅限控制台的实用程序。



外部终止某些过程始终是滥用,除非我们讨论过程控制的系统实用程序,这可以在紧急情况下进行,调试/故障排除和其他实验情况。这样做意味着处理没有为文明优雅方式终止的流程。



-SA


除了谢尔盖的建议,请查看如何优雅地终止进程?在堆栈溢出 [ ^ ]。

Hello,
so in my program i use EnumProcess to get all active processes and then compare this ID's with a file and if i find the same ID i terminate it ,
my problem now is how to terminate the program , should i use TerminateProcess ? is there any easier and safer method ? and ty

解决方案

Of course, you can use TerminateProcess, and this is not even so dangerous (compared to TerminateThread), but this is certainly a dirty thing to be used only in extreme cases; for example, OS can terminate faulty process. In regular use, the process should terminate itself, just by returning from its entry-point function. It should work this way even if you decide to terminate process on some fatal exception, but even this is rare and use mostly in simplest console-only utilities.

External termination of some process is always the abuse, unless we are talking about system utilities for process control, which can be done in emergency situations, debugging/troubleshooting and other experimental situations. Doing so means dealing with the processes not properly designed for termination in civilized graceful ways.

—SA


In addition to Sergey's advice, have a look at "How to gracefully terminate a process?" at Stack Overflow[^].


这篇关于C ++进程终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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