停留在GenerateConsoleCtrlEvent在C#与控制台应用程序 [英] Stuck on GenerateConsoleCtrlEvent in C# with console apps

查看:1329
本文介绍了停留在GenerateConsoleCtrlEvent在C#与控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最困难的时候试图得到这个工作,希望你们中的一个已经这样做过。

I'm having the hardest time trying to get this to work, hoping one of you has done this before.

我有正在运行的C#控制台应用程序一个子进程它继承其控制台。我想通过外部应用程序抓到一个CTRL-C沿到内部应用程序,以便它能够有机会很好地关闭传递。

I have a C# console app that is running a child process which inherits its console. I want a ctrl-c caught by the outer app to be passed along to the inner app so that it can have a chance to shut down nicely.

我有一些非常简单的代码。我启动一个进程,然后用WaitForExit(10)轮询它。我也有注册的CancelKeyPress处理程序,设置一个布尔为true时触发。轮询循环还会检查这一点,当它是真的,它调用GenerateConsoleCtrlEvent()(这是我通过的PInvoke映射)。

I have some very simple code. I start a Process, then poll it with WaitForExit(10). I also have a CancelKeyPress handler registered, which sets a bool to true when it fires. The polling loop also checks this, and when it's true, it calls GenerateConsoleCtrlEvent() (which I have mapped through pinvoke).

我试过很多的组合PARAMS到GenerateConsoleCtrlEvent()。 0或1为第一参数,并且0或子进程的第二个参数的ID。似乎没有任何工作。有时候,我得到一个错误的背部和Marshal.GetLastWin32Error()返回0,有时我真弄回来。但没有造成孩子应用接收一个CTRL-C

I've tried a lot of combinations of params to GenerateConsoleCtrlEvent(). 0 or 1 for the first param, and either 0 or the child process's ID for the second param. Nothing seems to work. Sometimes I get a false back and Marshal.GetLastWin32Error() returns 0, and sometimes I get true back. But none cause the child app to receive a ctrl-c.

要绝对确保,我写了一个测试C#应用程序是孩子应用程序,它打印出这是怎么回事它并验证它运行时不正确导致其退出的手动键入CTRL-C

To be absolutely sure, I wrote a test C# app to be the child app which prints out what's going on with it and verified that manually typing ctrl-c when it runs does properly cause it to quit.

我一直在敲我的头这几个小时。谁能给我在哪里与此去一些指点?

I've been banging my head against this for a couple hours. Can anyone give me some pointers on where to go with this?

推荐答案

不是很确定,这是一个不错的办法。如果子进程与CREATE_NEW_PROCESS_GROUP标志创建为这种方法只适用的CreateProcess()。在类的System.Diagnostics.Process但是并不支持这一点。

Not so sure this is a good approach. This only works if the child process is created with the CREATE_NEW_PROCESS_GROUP flag for CreateProcess(). The System.Diagnostics.Process class however does not support this.

使用从main()方法的返回值考虑。目前已经在Windows SDK中定义的唯一值,按Ctrl + C中止,STATUS_CONTROL_C_EXIT或0xC000013A。父进程可以从 Process.ExitCode 属性,返回代码。

Consider using the return value from the Main() method. There is already a unique value defined in the Windows SDK for Ctrl+C aborts, STATUS_CONTROL_C_EXIT or 0xC000013A. The parent process can get that return code from the Process.ExitCode property.

这篇关于停留在GenerateConsoleCtrlEvent在C#与控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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