【发布时间】:2011-07-01 09:33:04
【问题描述】:
简而言之,我正在使用 C# 进行科学计算,并且我编写了一个方法,它有一个 while 循环,可以运行到用户指定数量的步骤...实际上,这个方法可能需要很长时间才能执行(比如超过 5 个小时)。例如,当需要这么长时间时,我可能想停止按 Esc 键的方法。
当我读到一些关于破坏while 的内容时,它就像Boolean 标志或类似的东西一样简单。所以我想到了这样的事情:
public Double? Run(int n)
{
int i = 0;
while ((i < n) && (/* inputkey != ConsoleKey.Escape */))
{
// here goes the heavy computation thing
// and I need to read some "inputkey" as well to break this loop
i++;
}
// I'm not worried about the return statement, as it is easy to do...
// returns null if the user skipped the method by pressing Escape
// returns null if the method didn't converged
// returns the double value that the method calculated otherwise
}
嗯,这就是我到现在为止一直想知道的......那么,请你提供一些有用的想法吗?我如何等待用户输入(我考虑过Events,但我不确定如何在这里实现它,我认为如果我必须每隔一段时间听一个键,它会使代码变得更慢代码进入的步骤...
嗯,有什么想法或方法吗?
更新:我想我应该更好地描述这个问题。您给我的所有解决方案都可能解决我提出的这个问题,但我认为我对我的实际问题并不完全可靠。我不知道我是应该问另一个问题还是继续这个问题......
【问题讨论】:
标签: c# input while-loop user-input break