Skip to content

Instantly share code, notes, and snippets.

@arobson
Created Aug 24, 2011
Embed
What would you like to do?
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