UPDATE: In LinqPad 5 (five), is there any way to have a timed Util.ReadLine, that will allow me to wait for user input for X seconds, then return the default value?
Here's the synchronous code:
double mins = 0.0;
var input = Util.ReadLine("Timeout after how many minutes?", "360", new[] { "60", "120", "180", "240" });
double.TryParse(input, out mins);
I want to be to use the async version, Util.ReadLineAsync, like this:
var input = Util
.ReadLineAsync("Timeout after how many minutes?", "360", new[] { "60", "120", "180", "240" })
.Wait(TimeSpan.FromSeconds(30));
... but the task Wait command is a boolean return.
In versions prior to LINQPad 7, there is no async version of
Util.ReadLine(). And there's no safe way I can think of to cancel a ReadLine the way you're describing.Depending on your use case, you might want to use other, more asynchronous input methods. You can use controls from Windows Forms or WPF, for example, to show a textbox or a select box for a period of time. Or you can simply use variable declarations at the top of your script, and allow users to change those values before they execute your script.
Original Answer: The original question didn't specify a LINQPad Version, and this question may be googled by other users who have a later version, so I'm leaving it here, but the answer below only applies to LINQPad 7+.
Like many Async methods,
Util.ReadLineAsynctakes aCancellationToken. It will stop, with anOperationCanceledException, when theCancellationTokenyou provide is cancelled.