LINQPAD 5:如果在设置时间之后没有用户输入,则从异步读取线返回默认值

发布于 2025-02-05 02:49:04 字数 545 浏览 1 评论 0原文

Update 在Linqpad 5(五)中输入x秒,然后返回默认值?

这是同步代码:

double mins = 0.0;

var input = Util.ReadLine("Timeout after how many minutes?", "360", new[] { "60", "120", "180", "240" });

double.TryParse(input, out mins);

我想使用async版本,util.readlineasync,例如:

var input = Util
  .ReadLineAsync("Timeout after how many minutes?", "360", new[] { "60", "120", "180", "240" })
    .Wait(TimeSpan.FromSeconds(30));

...但是任务wait命令是布尔返回。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星光不落少年眉 2025-02-12 02:49:04

在LINQPAD 7之前的版本中,没有util.readline()的异步版本。而且,我没有安全的方法来取消按照您的描述方式进行阅读。

根据您的用例,您可能需要使用其他更多的异步输入方法。您可以使用Windows表单或WPF的控件在一段时间内显示文本框或选择框。或者,您可以简单地使用脚本顶部的变量声明,并允许用户在执行脚本之前更改这些值。

原始答案:原始问题没有指定linqpad版本,此问题可能是由其他版本的其他用户搜索的,所以我将其留在这里,但是以下答案仅适用到LinqPad 7+。

像许多异步方法一样,util.readlineasync采用concellationToken。它将停止,使用OperationCanceLedException,当您提供的concellationToken被取消时。

var input = await Util
  .ReadLineAsync("Timeout after how many minutes?", "360", new[] { "60", "120", "180", "240" },
    new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文