简体   繁体   中英

C How to terminate a thread blocked on ReadConsole()?

I'm trying to terminate a thread blocked on a ReadConsole() call from another thread. 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(). 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.

There is one solution which works under Windows 7, but I didn't test it specifically in a higher version of the Windows.

Additionally to FreeConsole which raises access violation under Windows 7, you can use:

CloseHandle(GetStdHandle(STD_INPUT_HANDLE))

which unblocks ReadConsole w/o exception.

You can later use CreateFile("\\\\\\\\.\\\\CON", ...) to reopen console input handle.

CAUTION: You must check the handle type: GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR before try to close it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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