Click here to Skip to main content
15,896,269 members
Articles / Desktop Programming / Windows Forms

Progress Reporting Framework

Rate me:
Please Sign up or sign in to vote.
4.82/5 (10 votes)
7 Sep 2011CPOL9 min read 31.3K   836   51  
Reporting Progress for Complex Algorithms
namespace ProgessExample
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using ProgressTracker;
    using System.ComponentModel;
    using System.Diagnostics;


    public interface IFibonacci
    {
        long Compute(int n, IReporter reporter);
    }


    public class MSDNStyleFibonacci : IFibonacci
    {
        private int numberToCompute = 0;
        private double highestPercentageReached = 0;
        private IReporterImpDirect directReporter = null;

        public long Compute(int n, IReporter reporter)
        {
            this.numberToCompute = n;
            this.highestPercentageReached = 0;
            this.directReporter = ReporterImpFactory.CreateDirect(reporter);

            return ComputeRecursive(n);
        }

        private long ComputeRecursive(int n)
        {
            long result = 0;

            if (n < 2)
            {
                result = n;
            }
            else
            {
                result = ComputeRecursive(n - 1) +
                         ComputeRecursive(n - 2);
            }

            // Report progress as a percentage of the total task.
            double percentComplete =
                ((double)n / (double)numberToCompute);

            if (percentComplete > highestPercentageReached)
            {
                highestPercentageReached = percentComplete;
                directReporter.Progress = percentComplete;
            }
           
            return result;
        }
    }

    public class TrackedFibonacci : IFibonacci
    {
        public long Compute(int n, IReporter updater)
        {
            return ComputeRecursive(n, updater);
        }

        private long ComputeRecursive(int n, IReporter reporter)
        {
            long result = 0;

            if (n < 2)
            {
                // var reporterImpl = ReporterImpFactory.CreateStep(reporter, new EvenSteps(1));
                result = n;
                // reporterImpl.Step();
            }
            else
            {
                // use golden ratio for relative effort: fib(n)/fib(n-1) = 1.618
                var reporterImpl = ReporterImpFactory.CreateStep(reporter, new IndividualSteps(1.618, 1));
                    
                result = ComputeRecursive(n - 1, reporterImpl.CreateSubReporter());
                result += ComputeRecursive(n - 2, reporterImpl.CreateSubReporter());
            }

            return result;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions