1

Does anybody know how to stop the Console.ReadLine method, when it's running on a different thread?

Right now I use a code (the loop part of it) that works like this:

while (true)
{
    while (!Console.KeyAvailable) Thread.Sleep(100);
    string line = Console.ReadLine();
}

It does work, but the problem is that as soon as I type a single character, the server won't shutdown.

Does anybody know how to achieve this? (Completely different approaches welcome too, as long as it allows me to read console lines, after I call a Start() method, and that it stops after a Stop() method call.)

Aidiakapi
  • 6,034
  • 4
  • 33
  • 62
  • This is a very old question. Yet if you came across this question as I did check out my answer at: https://stackoverflow.com/a/49511467/1729973 – Emad Mar 27 '18 at 11:19

3 Answers3

3

Console.ReadLine calls TextReader.ReadLine internally. This exits if the underlying stream is closed. If you call Console.In.Close() from the main thread it might work or you may not be able to close the console input stream. Try it and see.

Forget it, Console.In.Close() seems to be a no-op.

Update

This post has all sorts of methods for doing something very similar: How to add a Timeout to Console.ReadLine()?

(Deleted code because it didn't work)

Another update

I did some more research and this is what I found:

  • The console is in line mode.

  • The user cannot enter text in line mode until Read, ReadLine or Peek has been called.

  • Read, ReadLine and Peek block until the user has pressed Enter.

  • Hence we cannot let the console handle line input or we get the blocking problem.

  • So we must use KeyAvailable and ReadKey and manage deletions, etc. ourselves

This article has an example of handling ReadKey input: http://msdn.microsoft.com/en-us/library/system.consolekeyinfo.keychar.aspx

If you amend this function to always call KeyAvailable before calling ReadKey and sleep briefly if there's no input you can avoid blocking.

Community
  • 1
  • 1
arx
  • 16,686
  • 2
  • 44
  • 61
  • Tried that, but I wouldn't know how to restart it then... Do you know? – Aidiakapi Mar 10 '11 at 17:01
  • If you want one thread to detach from Console.In and another to start and reattach, how are you going to synchronize the input? If the first server had read part of a line, the second server wouldn't see it. – arx Mar 10 '11 at 17:05
  • No I mean, it's for a server, and you can only input data on certain moments. – Aidiakapi Mar 10 '11 at 17:06
  • Am I right in saying that you don't actually need the input on a separate thread; you're just looking for an interruptible wait on console input? – arx Mar 10 '11 at 17:24
  • There are a whole bunch of approaches for doing that here: http://stackoverflow.com/questions/57615/how-to-add-a-timeout-to-console-readline/ – arx Mar 10 '11 at 17:29
  • I'm sorry, but that's also reading key by key, I want to read a line. – Aidiakapi Mar 11 '11 at 13:00
2

Set the IsBackground property to true for the thread that is running this loop. That should allow the CLR to shutdown the application cleanly.

x0n
  • 51,312
  • 7
  • 89
  • 111
  • I don't want to shutdown the application, I want to stop the reading. And I know about the IsBackground property, but does it work for aborting too? Or only for shutting down? – Aidiakapi Mar 10 '11 at 16:45
1

Use KeyAvailable and ReadKey instead of ReadLine. Or possibly call Thread.Abort on the console consumer thread.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • Tried that, and stored it to a list, then converted it to a string. Didn't work too well, backspace crashes, so could you supply me with a working example of that? – Aidiakapi Mar 10 '11 at 16:54