I have a foreach loop that sends data to a GRPC API. I want my loop to send multiple requests at the same time but limit the number of requests to e.g. 10. My current code is the following:
foreach (var element in elements) { var x = new Thread(() => SendOverGrpc(element)); x.Start(); } But with that code, the software "immediately" sends all requests. How can I limit the number of requests to e.g. 10? As soon as one of my 10 requests is finished, I want to send the next one.
new Threadconsumes a big chunk of RAM for the stack and thread-local storage, which is why .NET comes with theThreadPool). Instead you should use proper async sockets which enables programs to achieve concurrent request processing very efficiently.