I recently came across an example of throttling threads for async/await calls. After analyzing and playing with the code on my machine, I came up with a slightly different way o
It's not much leaner, just a little bit. Usually, I avoid ContinueWith
in async
code because await
is cleaner and has more async
-friendly default semantics. Optimize for developer time first, then optimize for other considerations.
Your code does change the semantics slightly: in the original code, LongRunningTask
was executed from a thread pool context, and in your code it's executed from whatever the CallThrottledTasks
context is. Also, your code won't propagate exceptions from LongRunningTask
cleanly; Task<T>.Result
will wrap exceptions in AggregateException
, while await
will not do any wrapping.