Skip to content

Instantly share code, notes, and snippets.

@JayBazuzi
Last active August 29, 2015 13:57
Show Gist options
  • Save JayBazuzi/9867358 to your computer and use it in GitHub Desktop.
Save JayBazuzi/9867358 to your computer and use it in GitHub Desktop.
using (progress.Start(4 + items.Count()))
{
DoWork();
progress.ReportWork();
DoHeavyLifting(items);
progress.ReportWork(items.Count());
DoMoreWork();
progress.ReportWork(3);
}
var actionsWithCosts = new ActionsWithCostsList(progress)
{
DoWork,
{() => DoHeavyLifting(items), items.Count()},
{DoMoreWork, 3},
};
// ...
class ActionsWithCostsList : List<Tuple<Action, int>>
{
public new void Add(Action action, int cost = 1)
{
base.Add(Tuple.Create(action, cost));
}
}
static class ActionsWithCostsListExtensions
{
public static void ExecuteWithProgress(this ActionsWithCostsList @this, IProgress progress)
{
using (progress.Start(@this.Sum(t => t.Item2)))
{
foreach (Tuple<Action, int> step in @this)
{
step.Item1();
progress.ReportWork(step.Item2);
}
}
}
}
var actionsWithCosts = new ActionsWithCostsList()
{
() => i++,
() => Console.WriteLine(i),
};
interface IProgress
{
// Dispose to end
IDisposable Start(int totalSteps);
void ReportWork(int amount = 1);
}
interface IProgress
{
// Dispose to end
IDisposable Start(int totalSteps);
void ReportWork(int amount = 1);
IProgress CreateChild(int parentAmount = 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment