Make user choosing how many threads he want the program to use

前端 未结 2 1119
無奈伤痛
無奈伤痛 2021-01-28 07:14

I\'d like to make a program that read each line of a .txt file do something with each line.

I would like to user to be able to choose threads so if he choose for example

2条回答
  •  执念已碎
    2021-01-28 07:30

    Parallel.ForEach will run each loop instance in its own thread, and will also allow you to set the MaxDegreeOfParallelism (# of threads) to whatever you want.

            Parallel.ForEach(lines, new ParallelOptions{ MaxDegreeOfParallelism = 10 }, line =>
            {
                // do stuff with line
            });
    

提交回复
热议问题