简体   繁体   English

有什么方法可以防止控制台应用程序关闭?

[英]Is there any way to prevent console application to close?

Is there any way to prevent console application to close ? 有什么办法可以防止控制台应用程序关闭?

(that the user can't close the program) (用户无法关闭程序)

Take a look at the Win32 console API . 看一下Win32 控制台API

You could try creating a windows (non console app) and then create your own console window with AllocConsole(). 您可以尝试创建Windows(非控制台应用程序),然后使用AllocConsole()创建自己的控制台窗口。 As I haven't tried this I can't say if Windows will still try to kill your process. 因为我还没有尝试过,所以我不能说Windows是否仍将终止您的进程。

An alternative would be to create a background process/service to do the work. 另一种选择是创建一个后台进程/服务来完成这项工作。

Normally a console application can be aborted by pressing CTRL-C. 通常,可以通过按CTRL-C中止控制台应用程序。

You can prevent CTRL-C from aborting your application by setting the Console.TreatControlCAsInput property or handling the Console.CancelKeyPress event . 您可以通过设置Console.TreatControlCAsInput属性或处理Console.CancelKeyPress事件来防止CTRL-C中止应用程序。

Of course this won't stop the user from being able to kill your process, eg using Task Manager. 当然,这不会阻止用户杀死您的进程,例如使用任务管理器。

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

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