コード例 #1
0
 public void Iterate20()
 {
     using (Progress.BeginFixedTask(20))
     {
         for (int i = 0; i < 20; i++)
         {
             Progress.NextStep();
             DoNothing(i);
         }
         Progress.EndTask();
     }
 }
コード例 #2
0
        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);
        }
コード例 #3
0
        public void BackgroundTask()
        {
            const int primary = 10;
            const int second  = 10;
            const int third   = 10;

            while (true)
            {
                //Dispatcher.Invoke((Action)ProgressReset);
                using (Progress.BeginFixedTask(primary).SetTaskKey("Primary"))
                {
                    for (int i = 0; i < primary; i++)
                    {
                        Progress.NextStep();


                        using (Progress.BeginFixedTask(second).SetTaskKey("Second"))
                        {
                            for (int j = 0; j < second; j++)
                            {
                                Progress.NextStep();



                                using (Progress.BeginFixedTask(third).SetTaskKey("Third"))
                                {
                                    for (int k = 0; k < third; k++)
                                    {
                                        Progress.NextStep();


                                        if (isExiting)
                                        {
                                            return;
                                        }


                                        System.Threading.Thread.Sleep(delay);
                                    }
                                    Progress.EndTask();
                                }
                            }
                            Progress.EndTask();
                        }
                    }
                    Progress.EndTask();
                }
            }
        }
コード例 #4
0
 public void TestNormal()
 {
     currentProgress = -1f;
     // Normal for-loop:
     using (Progress.BeginFixedTask(10).SetCallback(AssertProgressIsGrowing, ProgressDepth.Unlimited))
     {
         for (int i = 0; i < 10; i++)
         {
             Progress.NextStep();
             AssertCurrentProgress(0.0f + 10f * i);
         }
         Progress.EndTask();
     }
     AssertCurrentProgress(100f);
 }
コード例 #5
0
        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();
                }
            }
        }
コード例 #6
0
        public void TestUnknownTimer()
        {
            currentProgress = -1f;
            using (Progress.BeginWeightedTask(10f, 80f, 10f).SetCallback(AssertProgressIsGrowing))
            {
                Progress.SetTaskKey("Stall 1 second").NextStep();
                Progress.BeginFixedTask(10);
                for (int i = 0; i < 10; i++)
                {
                    Progress.NextStep();
                    System.Threading.Thread.Sleep(100);
                }
                Progress.EndTask();


                // Start a task that takes unknown time:
                Progress.SetTaskKey("Unknown for 8+ seconds").NextStep();
                using (Progress.BeginUnknownTask(8, .90f))
                {
                    System.Threading.Thread.Sleep(8000);
                    System.Threading.Thread.Sleep(3000); // Take some extra time!
                    Progress.EndTask();
                }


                Progress.SetTaskKey("Stall another second").NextStep();
                using (Progress.BeginFixedTask(10))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Progress.NextStep();
                        System.Threading.Thread.Sleep(100);
                    }
                    Progress.EndTask();
                }
                Progress.EndTask();
            }
        }
コード例 #7
0
        public void Test_OpenProgressWithError()
        {
            currentProgress = -1;
            using (Progress.BeginFixedTask(5).SetCallback(AssertProgressIsGrowing))
            {
                Progress.NextStep();

                try
                {
                    Progress.BeginFixedTask(5);
                    Progress.NextStep();

                    Progress.BeginFixedTask(5);
                    Progress.NextStep();

                    Progress.BeginFixedTask(5);
                    Progress.NextStep();

                    throw new Exception("What happens if we don't use \"using\" blocks and we forget to call EndTask?");

                    Progress.EndTask();
                    Progress.EndTask();
                    Progress.EndTask();
                }
                catch (Exception)
                {
                }

                // Dispose the final task,
                // which should emit several warnings
                // but will dispose all nested tasks.
            }

            // Make sure the progress tasks are all disposed:
            Assert.IsNull(ProgressTask.CurrentTask);
        }
コード例 #8
0
        public void Test_ThreadSafety()
        {
            currentProgress = -1;

            ProgressTask progress = null;

            // Create a background thread:
            const int count            = 100000;
            Action    backgroundThread = () => {
                using (progress = Progress.BeginFixedTask(count).EnablePolling((ProgressDepth)4))
                {
                    for (int i = 0; i < count; i++)
                    {
                        Progress.NextStep();
                        // Fake a sub-task that ends quickly:
                        Progress.BeginFixedTask(2);
                        Progress.NextStep();
                        Progress.NextStep();
                        Progress.EndTask();
                    }
                }
            };

            // Start the background thread:
            var async = backgroundThread.BeginInvoke(null, null);

            // Wait for the BG process to start:
            while (progress == null)
            {
                Thread.Sleep(1);
            }

            int   maxDepth      = -1;
            int   loops         = 0;
            float totalProgress = 0f;

            while (totalProgress < 1.0f)
            {
                var progressInfo = progress.CurrentProgress;

                AssertProgressIsGrowing(progressInfo);
                totalProgress = progressInfo.TotalProgress;
                if (maxDepth < progressInfo.CurrentDepth)
                {
                    maxDepth = progressInfo.CurrentDepth;
                }


                loops++;
                if (loops % 100 == 0)
                {
                    // Let the bg task queue up,
                    // and then make sure the CurrentDepth
                    // is zero:
                    Thread.Sleep(10);
                    progressInfo = progress.CurrentProgress;
                    Assert.That(progressInfo.CurrentDepth, Is.EqualTo(0));
                }
            }

            // Make sure that we received at least 1 nested event:
            Assert.That(maxDepth, Is.EqualTo(1));

            // Clean up
            backgroundThread.EndInvoke(async);
        }