Last active
August 29, 2015 13:57
-
-
Save JayBazuzi/9867358 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (progress.Start(4 + items.Count())) | |
{ | |
DoWork(); | |
progress.ReportWork(); | |
DoHeavyLifting(items); | |
progress.ReportWork(items.Count()); | |
DoMoreWork(); | |
progress.ReportWork(3); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var actionsWithCosts = new ActionsWithCostsList() | |
{ | |
() => i++, | |
() => Console.WriteLine(i), | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IProgress | |
{ | |
// Dispose to end | |
IDisposable Start(int totalSteps); | |
void ReportWork(int amount = 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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