Is it possible to stop the Console.ReadLine() programmatically?
I have a console application: the much of the logic runs on a different thread and in the main thread I accept input using Console.ReadLine(). I’d like to stop reading from console when the separated thread stop running.
How can I achieve this?
UPDATE: this technique is no longer reliable on Windows 10. Don’t use it please.
Fairly heavy implementation changes in Win10 to make a console act more like a terminal. No doubt to assist in the new Linux sub-system. One (unintended?) side-effect is that CloseHandle() deadlocks until a read is completed, killing this approach dead. I’ll leave the original post in place, only because it might help somebody to find an alternative.
UPDATE2: Look at wischi’s answer for a decent alternative.
It’s possible, you have to jerk the floor mat by closing the stdin stream. This program demonstrates the idea: