我正在 Parallel.For 回圈中運行模擬,但我需要在一段時間后停止,即使它沒有運行所有迭代。
我需要能夠在 Parallel.For 中執行以下操作。
for(int i = 0; i < 1_000_000 || DateTime.Now.Subtract(StartTime) >= MaxDuration; i )
{
// do the thing
}
uj5u.com熱心網友回復:
正如 Heinzi 在評論中提到的,在檢查條件后呼叫ParallelLoopState.Break()有效
Parallel.For(1, 1_000_000, (i, state) =>
{
//do the thing
if (DateTime.Now-startTime>=maxDuration)
{
state.Break();
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482865.html
