2

I'm writing a tool that sends queries to an azure table, the amount of queries depends on the user. I want to send queries in parallel but only up to a given number (I don't want to send all 100 queries at once). Is there any built in mechanism I can use to sent say up to 20 queries in parallel each time?

I know there is Parallel.ForEach which can be limited using ParallelOptions.MaxDegreeOfParallelism, but for asynchronous operation like mine this will just send all the queries really fast and my tool will handle all 100 callbacks at once.

1
  • Why does limiting MaxDegreeOfParallelism not work for you? Why do you buffer the requests- do you know for sure the service is unable to process all of them? how do you consume the service? usually he framework provides you with built in mechanisms to handle asynchronouse I/O like that Commented Jan 12, 2014 at 17:05

1 Answer 1

4

You should use SemaphoreSlim. It's especially nice in the case of async operations because it has WaitAsync that returns a task you can await on. The first 20 will go right through, and the rest will asynchronously wait for an operation to end so they can start.

SemaphoreSlim _semaphore = new SemaphoreSlim(20); async Task DoSomethingAsync() { await _semaphore.WaitAsync(); try { // possibly async operations limited to 20 } finally { _semaphore.Release(); } } 

Usage:

for(int i=0; i < 100; i++) { await DoSomethingAsync(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.