C#中的CLOCKS_PER_SEC等价物

时间:2017-10-17 11:16:11

标签: c# c++

我在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)
{

}

1 个答案:

答案 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
    }
}
相关问题