Create a gist now

Instantly share code, notes, and snippets.

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