1

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.

TechSpud
  • 3,418
  • 1
  • 27
  • 35

1 Answers1

1

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.ReadLineAsync takes a CancellationToken. It will stop, with an OperationCanceledException, when the CancellationToken you provide is cancelled.

var input = await Util
  .ReadLineAsync("Timeout after how many minutes?", "360", new[] { "60", "120", "180", "240" },
    new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token);
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • 1
    This is only available in LINQPad 7. – Enigmativity Jun 08 '22 at 00:15
  • Thank you for your answer @striplingwarrior, but as enigmativity alludes to, this is not available in LinqPad 5. Is there any way I can mimic this in LinqPad 5? Also realised that I didn't specify the version in the question - so I'll update the question now – TechSpud Jun 08 '22 at 07:52
  • Thanks for the update, @striplingwarrior. Was hoping to be able to wrap the async call to `ReadLineAsync ` in some kind of Task method. Back to the drawing board! :) – TechSpud Jun 09 '22 at 09:01