Click here to Skip to main content
15,897,371 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using ProgressTracker;

namespace ProgessExample
{
    public partial class Form1 : Form
    {
        private List<IFibonacci> FibList = new List<IFibonacci>();
        private Binding currentProgressBinding = null;

        public Form1()
        {
            this.InitializeComponent();

            FibList.Add(new MSDNStyleFibonacci());
            FibList.Add(new TrackedFibonacci());
            comboFib.DataSource = FibList;
            textBoxParamN.Text = "30";
        }

        private void Go_Click(object sender, EventArgs e)
        {
            // If a tracked algorithm is already running, disconnect it from
            // the progress bar
            if (this.currentProgressBinding != null)
            {
                this.barProgress.DataBindings.Remove(this.currentProgressBinding);
                this.currentProgressBinding = null;
            }
                    
            
            {
                int nUpdates = 0;

                // Instantiate the newscaster object:
                NewsCaster newsCaster = new NewsCaster();

                // Optionally show the results in a diagram. Storing the data for
                // the diagrams may take some time, so expect that the algorithm will
                // take longer if this is enabled.
                if (Control.ModifierKeys == Keys.Control) newsCaster.EnableProgressResultDlg = true;

                // Add a databinding for the progress bar
                this.currentProgressBinding = this.barProgress.DataBindings.Add("Value", newsCaster, "Progress");

                // Use the ProperyChanges event for text box updates (this is just to
                // explain the difference)
                newsCaster.PropertyChanged += (_sender, _e) =>
                {
                    lblUpdates.Text = nUpdates.ToString();
                    lblTimeElapsed.Text = newsCaster.TimeElapsed.ToString();
                    lblTimeEstimated.Text = newsCaster.TimeEstimated.ToString();
                    
                    nUpdates++;
                };

                newsCaster.Finished += () => {
                    // The Finished event may be used to perform an operation in the UI
                    // thread once the tracked task finishs.
                };

                IFibonacci fib = comboFib.SelectedItem as IFibonacci;

                int paramN = Convert.ToInt32(textBoxParamN.Text);
                if (paramN < 2) paramN = 2;
                if (paramN > 40) paramN = 40;
                textBoxParamN.Text = paramN.ToString();

                ThreadPool.QueueUserWorkItem((state) =>
                {
                    // Pass the newly created root reporter to
                    // the tracked algorithm
                    fib.Compute(paramN, 
                        this.cbProgress.Checked ? 
                            newsCaster.CreateReporter()
                                :
                            null);

                    // Signal Finalization (will disallow further progress
                    // updates and will raise the Finished event in the UI thread)
                    newsCaster.Finish();
                });
            }
        }
    }
}

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