-1

I'm trying to do the following to prevent my console app do be stuck on Console.ReadLine() during termination.

await Process.GetCurrentProcess().StandardInput.WriteLineAsync();

but I'm getting the InvalidOperationException StandardIn is not redirected.

The attempt based on John's comment also fails with an exception Stream was not writable:

using (var stdInStream = Console.OpenStandardInput())
using (var stdInWriter = new StreamWriter(stdInStream))
    await stdInWriter.WriteLineAsync();

Is there an easy way to achieve this?

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
  • Not `Console.OpenStandardInput()` or `Console.SetIn(textReader)`? – ProgrammingLlama Jul 16 '20 at 02:21
  • Perhaps you need a [timeout](https://stackoverflow.com/questions/57615/how-to-add-a-timeout-to-console-readline)? – ProgrammingLlama Jul 16 '20 at 02:22
  • @John Attempting to write to the stream returned by `Console.OpenStandardInput()` throws an exception saying `Stream was not writable.`. The code handing the console is not something I can easily change, I intentionally did not ask how to augment the ReadLine part. – Cobra_Fast Jul 16 '20 at 02:32
  • @Cobra_Fast Did [this](https://stackoverflow.com/a/10208589) in particular not work? – dxiv Jul 16 '20 at 03:13

1 Answers1

0

On windows, Console.ReadLine ends up blocking on a Kernel32 ReadLine call. While there might be a way to interrupt that call and cause it to return, writing to your own input doesn't seem like the right answer.

You might be able to call CancelSynchronousIo, or just exit your own process. Neither of which is ideal.

Edit; a potentially related github issue

Jeremy Lakeman
  • 9,515
  • 25
  • 29