Skip to content

Instantly share code, notes, and snippets.

@arobson
Created August 24, 2011 04:52
Show Gist options
  • Save arobson/1167335 to your computer and use it in GitHub Desktop.
Save arobson/1167335 to your computer and use it in GitHub Desktop.
Improved Approach To Limiting Asynchronous Calls with Rx
Action<IList<XElement>> saveAction = SaveChunk;
var loader = new BulkPostLoader(@"e:\stackoverflow\062010 so\posts.xml");
var batches = loader.BufferWithCount(5000);
var results = batches.Select(x => saveAction.BeginInvoke(x, null, null));
results
.Aggregate(new HashSet<IAsyncResult>(), (set, result) =>
{
set.RemoveWhere(x => x.IsCompleted);
set.Add(result);
if(set.Count > 5)
{
var inProcess = set.Where(x => !x.IsCompleted).FirstOrDefault();
if(inProcess != null)
{
inProcess.AsyncWaitHandle.WaitOne();
}
}
return set;
}
.Subscribe(x => {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment