我在C ++中使用此代码段并且很难将其转换为C#
clock_t now = clock();
myStartTime = start;
myTimeLimit = 5 // in seconds
for (int depth = 2; ((double)(now - myStartTime)) / (double)CLOCKS_PER_SEC < myTimeLimit; depth += 2)
{
//
}
这是我应该怎么做的?
var now = DateTime.Now;
myStartTime = start;
myTimeLimit = 5;
for (int depth = 2; (now - myStartTime).TotalSeconds < myTimeLimit; depth += 2)
{
}
答案 0 :(得分:2)
您可以使用TotalMinutes作为实现此目标的更好选择。例如
var clt = new CancellationTokenSource(5000);
Task.Run(() => DoSomething(clt.Token));
private static void DoSomething(CancellationToken cltToken)
{
for (int depth = 2; !cltToken.IsCancellationRequested; depth += 2)
{
// . . .
}
if (cltToken.IsCancellationRequested) {
// Time limit reached before finding best move at this depth
}
}