Skip to content

Instantly share code, notes, and snippets.

@sixeyed
Created December 9, 2013 16:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixeyed/7875555 to your computer and use it in GitHub Desktop.
Save sixeyed/7875555 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Sixeyed.Framework
{
public class TaskBatcher : IDisposable
{
private int _batchSize;
private int _batchIndex = 0;
private List<Task> _tasks = new List<Task>();
public TaskBatcher(int batchSize)
{
_batchSize = batchSize;
}
public void Add(Action action)
{
if (_batchIndex == _batchSize)
{
Task.WaitAll(_tasks.ToArray());
GC.Collect();
_batchIndex = 0;
_tasks = new List<Task>();
}
_tasks.Add(Task.Factory.StartNew(action));
_batchIndex++;
}
public void Dispose()
{
if (_batchIndex > 0)
{
Task.WaitAll(_tasks.ToArray());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment