public void Iterate10() { var items = new[] {new {X = 10}, new {X = 20}, new {X = 30}, new {X = 40}, new {X = 50}, new {X = 60}, new {X = 70}, new {X = 80}, new {X = 90}, new {X = 100}}; foreach (var item in items.WithProgress()) { DoNothing(item); } }
public void TestCallbackDepth() { var justFine = false; ProgressChangedCallback callback = (p) =>{ if (p.Any(pi => pi.TaskKey == "Too Deep!")) Assert.Fail("'Too Deep!' invoked a callback!"); if (p.Any(pi => pi.TaskKey == "Just fine")) justFine = true; }; var items = new[]{1,2,3,4,5}; // Here, we set the callback with a maximum depth of 3: foreach (var zero in items.WithProgress().SetCallback(callback, (ProgressDepth)3)) { foreach (var one in items.WithProgress()) { foreach (var two in items.WithProgress()) { // This progress is 3-deep, so it should work Just fine: foreach (var three in items.WithProgress().SetTaskKey("Just fine")) { // This progress is 4-deep, so it shouldn't fire the callback: foreach (var four in items.WithProgress().SetTaskKey("Too Deep!")) { DoNothing(four); } } } } } if (justFine == false) { Assert.Fail("'Just fine' did not invoke a callback!"); } }
public void Iterate550() { var items = new[] {new {X = 10}, new {X = 20}, new {X = 30}, new {X = 40}, new {X = 50}, new {X = 60}, new {X = 70}, new {X = 80}, new {X = 90}, new {X = 100}}; var weights = items.Select(i => (float)i.X).ToArray(); foreach (var x in items.WithProgress(weights)) { using (Progress.BeginFixedTask(x.X)) { for (int i = 0; i < x.X; i++) { Progress.NextStep(); DoNothing(i); } Progress.EndTask(); } } }
public void Test_ProgressWithError() { try { currentProgress = -1; var tenItems = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; foreach (var i in tenItems.WithProgress().SetCallback(AssertProgressIsGrowing)) { foreach (var j in tenItems.WithProgress()) { if (i == 5 && j == 5) { AssertCurrentProgress(44f); // Throw an error in the middle of this task: throw new OperationCanceledException("Die Progress Die!"); } } } Assert.Fail("Code shouldn't get to this point."); } catch (OperationCanceledException) { // Make sure the current progress hasn't changed since the error: AssertCurrentProgress(44f); } }
public void TestNested() { currentProgress = -1f; // Begin main task with 4 sections, each one taking longer: using (Progress.BeginWeightedTask(new[] { 10f, 20f, 30f, 40f }).SetCallback(AssertProgressIsGrowing)) { Progress.NextStep(); // Advance the main task // Normal for-loop: Progress.BeginFixedTask(10); for (int i = 0; i < 10; i++) { Progress.NextStep(); AssertCurrentProgress((0.0f) + i); } Progress.EndTask(); Progress.NextStep(); // Advance the main task // "Iterate" an array of 20 items: var twenty = new[]{1f,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; foreach (var i in twenty.WithProgress()) { AssertCurrentProgress((10f) + (i-1f)); } Progress.NextStep(); // Advance the main task // "Iterate" a weighted array of 30 items: var thirty = new[]{1f,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; foreach (var i in thirty.WithProgress(thirty)) { // Calculate the step progress: var x = i - 1f; x = 30f * x*(x+1f)/930f; AssertCurrentProgress((10f + 20f) + x); } Progress.NextStep(); // Advance the main task // Normal for-loop, with a "using" block instead of EndTask. using (Progress.BeginFixedTask(40)) { for (int i = 0; i < 40; i++) { Progress.NextStep(); AssertCurrentProgress(60f + i); } Progress.EndTask(); } AssertCurrentProgress(100f); Progress.EndTask(); } AssertCurrentProgress(100f); }