Windows ctrl + c signals do not fire

I am working on an application that runs powershell scripts to configure and run maintenance processes (not formal Windows services). I want to be able to send signals ctrl+ to cthese powershell processes and their child processes so that they can be closed cleanly.

When I create a powershell process, I install CREATE_NEW_PROCESS_GROUP

which implicitly calls SetConsoleCtrlHandler(NULL,TRUE)

, which effectively disables ctrl+ cfor the created process and its children. I cannot use ctrl+ breakbecause that puts powershell into debug mode. So in the powershell process that I start, I call SetConsoleCtrlHandler(NULL,FALSE)

to re-enable ctrl+ cand my application then calls GenerateConsoleCtrlEvent

to send ctrl+ events cto the process group in the workspace when I want to stop that process tree. This works great, but with one major drawback.

If I use type ctrl + c in the console where my application is running, my application intercepts the ctrl+ cand then calls GenerateConsoleCtrlEvent

powershell for each process. This seems to work well, but ONLY the first time.

After exiting my application, it seems that ctrl+ is cnow disabled for any program running on that console. If I run PING

or my own application, the ctrl+ cdoes nothing and I'm not sure how to debug further.

Interestingly, sometimes I see handles in the process handler for dead powershell processes in conhost.exe. If I close these handles in the process handler, SOMETIMES ctrl+ is returned c. So I suspect some handle leaks, but this observation is inconsistent and I am trying to free up my process handlers in my application.

Any thoughts on what might be causing my console signals ctrl+ to be swallowed c?

+3


source to share


1 answer


It looks like launching GenerateConsoleCtrlEvent

after the user has called ctrl+ cis what puts the shell into this weird state. When the user enters ctrl+ c, I really don't need my app to call GenerateConsoleCtrlEvent

because the GenerateConsoleCtrlEvent broadcasts all processes to the console. Unfortunately, I have a forego application GenerateConsoleCtrlEvent

, when it intercepts the ctrl+ cit is easier said than done, but not impossible.

His concern is that I haven't found any documentation or information that shooting GenerateConsoleCtrlEvent

after the entered ctrl+ chas this odd effect, but maybe this post will help someone else.



If anyone has more details on why this condition is triggered, or tips for other ways to avoid it and make the shell more resilient GenerateConsoleCtrlEvent

, post another answer here.

+2


source







All Articles