Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / C#

Dealing with Progressive Operations

Rate me:
Please Sign up or sign in to vote.
4.92/5 (26 votes)
10 May 2010CPOL30 min read 37.9K   318   60  
Through a clean OOP solution to deal with progressive operations, I will implicitly show you how OOP principles can work together to make a full, clean solution.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using Erik.Utilities.Interfaces;

namespace MultipleThreadsSample
{
    public partial class frmSearch : Form
    {
        IFindInFilesPO _po;

        EventHandler onProgressInvoker;
        EventHandler onStartInvoker;
        EventHandler onEndInvoker;
        EventHandler onAbortInvoker;
        FileFoundEventHandler onFileFoundInvoker;

        EventHandler onSubOperationStartInvoker;
        EventHandler onSubOperationEndOrAbortInvoker;

        public frmSearch()
        {
            InitializeComponent();

            Text = Program.NewTitle;

            onStartInvoker = new EventHandler(_po_OperationStart);
            onProgressInvoker = new EventHandler(_po_OperationProgress);
            onEndInvoker = new EventHandler(_po_OperationEnd);
            onAbortInvoker = new EventHandler(_po_Aborted);

            onFileFoundInvoker = new FileFoundEventHandler(_po_FileFound);

            onSubOperationStartInvoker = new EventHandler(subOperation_OperationStart);
            onSubOperationEndOrAbortInvoker = new EventHandler(subOperation_OperationEnd);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "Abort")
                _po.Abort();
            else
            {
                lstFiles.Items.Clear();
                lstFolders.Items.Clear();

                lblStatus.Text = "Preparing to search";
                ChangeControlsEnabled(false);

                Thread th = new Thread(new ThreadStart(PrepareToSearch));
                th.Start();
            }
        }

        void ChangeControlsEnabled(bool enable)
        {
            txtSearchPath.Enabled = enable;
            txtSearchText.Enabled = enable;
            btnBrowse.Enabled = enable;
            btnStart.Enabled = enable;
            chkIncludeSubFolders.Enabled = enable;
        }

        void PrepareToSearch()
        {
            if (!chkIncludeSubFolders.Checked)
                _po = new FindInFilesPO(txtSearchText.Text, txtSearchPath.Text);
            else
            {
                List<IFindInFilesPO> lst = new List<IFindInFilesPO>();
                FillOperationList(lst, txtSearchPath.Text);

                _po = new CompositeFindInFilesPO(lst, txtSearchText.Text, txtSearchPath.Text);
            }

            _po.FileFound += onFileFoundInvoker;
            _po.OperationStart += onStartInvoker;
            _po.OperationProgress += onProgressInvoker;
            _po.OperationEnd += onEndInvoker;
            _po.Aborted += onAbortInvoker;

            _po.Start();
        }

        void FillOperationList(List<IFindInFilesPO> lst, string path)
        {
            if (Directory.GetFiles(path).Length > 0)
            {
                IFindInFilesPO subOperation = new FindInFilesPO(txtSearchText.Text, path);
                lst.Add(subOperation);

                subOperation.OperationStart += onSubOperationStartInvoker;
                subOperation.OperationEnd += onSubOperationEndOrAbortInvoker;
                subOperation.Aborted += onSubOperationEndOrAbortInvoker;
            }

            string[] dirs = Directory.GetDirectories(path);
            foreach (string dir in dirs)
                FillOperationList(lst, dir);
        }

        void subOperation_OperationEnd(object sender, EventArgs e)
        {
            if (InvokeRequired)
                Invoke(onSubOperationEndOrAbortInvoker, sender, e);
            else
            {
                IFindInFilesPO po = sender as IFindInFilesPO;
                lstFolders.Items.Remove(po.SearchPath);

                pgRunningThreads.Text =
                    string.Format("Folders being processed: {0}", lstFolders.Items.Count);
            }
        }

        void subOperation_OperationStart(object sender, EventArgs e)
        {
            if (InvokeRequired)
                Invoke(onSubOperationStartInvoker, sender, e);
            else
            {
                IFindInFilesPO po = sender as IFindInFilesPO;
                lstFolders.Items.Add(po.SearchPath);

                pgRunningThreads.Text = 
                    string.Format("Folders being processed: {0}", lstFolders.Items.Count);
            }
        }

        void _po_Aborted(object sender, EventArgs e)
        {
            if (InvokeRequired)
                Invoke(onAbortInvoker, sender, e);
            else
            {
                lblStatus.Text = "Operation aborted";
                pgb.Visible = false;

                btnStart.Text = "Start";
                ChangeControlsEnabled(true);
            }
        }

        void _po_OperationEnd(object sender, EventArgs e)
        {
            if (InvokeRequired)
                Invoke(onEndInvoker, sender, e);
            else
            {
                lblStatus.Text = "Operation completed";
                btnStart.Text = "Start";
                pgb.Visible = false;

                ChangeControlsEnabled(true);
            }
        }

        void _po_OperationProgress(object sender, EventArgs e)
        {
            if (InvokeRequired)
                Invoke(onProgressInvoker, sender, e);
            else
            {
                pgb.Value = _po.CurrentProgress;
                Application.DoEvents();
            }
        }

        void _po_OperationStart(object sender, EventArgs e)
        {
            if (InvokeRequired)
                Invoke(onStartInvoker, sender, e);
            else
            {
                btnStart.Text = "Abort";
                btnStart.Enabled = true;
                pgb.Visible = true;
            }
        }

        void _po_FileFound(object sender, FileFoundEventArgs e)
        {
            if (InvokeRequired)
                Invoke(onFileFoundInvoker, sender, e);
            else
                lstFiles.Items.Add(e.FileName);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (dlg.ShowDialog() == DialogResult.OK)
                txtSearchPath.Text = dlg.SelectedPath;
        }

        private void frmSearch_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (_po != null)
                    _po.Abort();
            }
            catch { }
        }
    }
}

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
Technical Lead
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions