How to configure a maximum number of threads in a Parallel.For

后端 未结 3 800
执念已碎
执念已碎 2021-01-01 12:00

This is the example microsoft presents for the parallel for, and I\'d like to know how configure a maximum number of threads for this code.

     // A basic m         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 12:21

    You need to specify a ParallelOptions value with a MaxDegreeOfParallelism:

    For example:

    Parallel.For(0, 10, new ParallelOptions { MaxDegreeOfParallelism = 4 }, count =>
    {
        Console.WriteLine(count);
    });
    

提交回复
热议问题