简体   繁体   中英

C# equivalent of Python's ThreadPool

I want to ask is there any alternative for the Python's Threadpool function. I am new to C#. I have a function that takes a string as input and i have a list of inputs. What i want is to execute the function in multiple threads(max. 12) such that to every thread one string input from the list is passed and if the size of the list is greater than the number of threads then it waits for any one the thread to complete its execution and then start the function again in the thread with the next input. Hope the question makes sense to you all.

I have done this in Python:

def myfunc(input):
 print(input)

if__name__ == "__main__":
 inputs = [1,12,2,3,4,5,6,7,8,9,1,0,1,2,3,4,5,6,4,8,97,7]
 ThreadPool(12).map(myfunc, inputs)
static void Main()
{
    var inputs = new[] { 1, 12, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 2, 3, 4, 5, 6, 4, 8, 97, 7 };
    Parallel.ForEach(inputs, new ParallelOptions { MaxDegreeOfParallelism = 12 }, MyFunc);
}
static void MyFunc(int value) { /*...*/ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM