Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Dealing with Progressive Operations

, 10 May 2010
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)

About the Author

_Erik_
Technical Lead
Spain Spain
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 10 May 2010
Article Copyright 2010 by _Erik_
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid