Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is not the most efficient method, but if this is for a desktop application, efficiency isn't your primary concern anyway. So, unless you are actually seeing performance degradation from this code, you shouldn't change it.</p> <p>That said, I would have approached this differently.</p> <p>You're using the TPL to parallelize I/O-bound operations. You're using <a href="http://msdn.microsoft.com/en-us/library/ff963551.aspx" rel="nofollow">dynamic parallelism</a>, the most complex kind; as Jeff Mercado commented, your code would be simpler and slightly more efficient if you used a higher-level parallelism abstraction such as <a href="http://msdn.microsoft.com/en-us/library/ff963552.aspx" rel="nofollow"><code>Parallel</code> or <code>PLINQ</code></a>).</p> <p>However, any <em>parallel</em> approach is going to waste thread pool threads by blocking them. Since this is I/O-bound, I would recommend using <code>async</code>/<code>await</code> to make them concurrent.</p> <p>If you want to do simple throttling, you can use <code>SemaphoreSlim</code>. I don't think you need to do throttling like this in addition to your subsets, but if you want an <code>async</code> equivalent to your existing code, it would look something like this:</p> <pre><code>public Task SearchAsync() { var subsets = CreateSubsets(originalList); return Task.WhenAll(subsets.Select(subset =&gt; SearchSubsetAsync(subset))); } private Task SearchSubsetAsync(List&lt;SearchCriteria&gt; subset) { var semaphore = new SemaphoreSlim(3); return Task.WhenAll(subset.Select(criteria =&gt; SearchCriteriaAsync(criteria, semaphore))); } private async Task SearchCriteriaAsync(SearchCriteria criteria, SemaphoreSlim semaphore) { await semaphore.WaitAsync(); try { // SearchForCriteriaAsync uses HttpClient (async). var results = await SearchForCriteriaAsync(criteria); // Consider returning results rather than processing them here. } finally { semaphore.Release(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload