我在Parallel.For循环中运行一个模拟,但是我需要在一定时间之后停止,即使它没有运行所有的迭代。
我需要能够在Parallel.For中完成以下操作。
for(int i = 0; i < 1_000_000 || DateTime.Now.Subtract(StartTime) >= MaxDuration; i++)
{
// do the thing
}发布于 2022-05-28 16:15:14
正如Heinzi在注释中提到的,调用ParallelLoopState.Break()是在检查了条件之后工作的。
Parallel.For(1, 1_000_000, (i, state) =>
{
//do the thing
if (DateTime.Now-startTime>=maxDuration)
{
state.Break();
}
});https://stackoverflow.com/questions/72417043
复制相似问题