[英]C How to terminate a thread blocked on ReadConsole()?
I'm trying to terminate a thread blocked on a ReadConsole() call from another thread.我正在尝试终止在另一个线程的 ReadConsole() 调用上阻塞的线程。 It doesn't work with terminateThread(i know it's not a good way to stop it) because when a restart that thread, somehow the thread is still there trying to return ReadConsole().它不适用于 terminateThread(我知道这不是停止它的好方法),因为当重新启动该线程时,该线程仍然以某种方式试图返回 ReadConsole()。 I've also tried to set an event from the other thread e wait for it on the other side, but i can't still figure out how to stop it.我还尝试从另一个线程设置一个事件 e 在另一端等待它,但我仍然无法弄清楚如何阻止它。
There is one solution which works under Windows 7, but I didn't test it specifically in a higher version of the Windows.有一种解决方案可以在 Windows 7 下运行,但我没有在更高版本的 Windows 中专门对其进行测试。
Additionally to FreeConsole
which raises access violation under Windows 7, you can use:除了在 Windows 7 下引发访问冲突的FreeConsole
,您还可以使用:
CloseHandle(GetStdHandle(STD_INPUT_HANDLE))
which unblocks ReadConsole
w/o exception.它解除了ReadConsole
异常。
You can later use CreateFile("\\\\\\\\.\\\\CON", ...)
to reopen console input handle.您可以稍后使用CreateFile("\\\\\\\\.\\\\CON", ...)
重新打开控制台输入句柄。
CAUTION: You must check the handle type: GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR
before try to close it.注意:在尝试关闭它之前,您必须检查句柄类型: GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.