Click here to Skip to main content
15,886,664 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 38K   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.Linq;
using System.Text;
using System.IO;
using System.Threading;
using Erik.Utilities.Bases;
using Erik.Utilities.Interfaces;

namespace MultipleThreadsSample
{
    public class FindInFilesPO :
        BaseProgressiveOperation, IFindInFilesPO
    {
        string[] _files;
        string _text;
        string _path;
        List<string> _errors;

        bool _abort;
        Mutex _mutexAbort;

        public FindInFilesPO(string textToFind, string path)
        {
            _files = Directory.GetFiles(path);
            _totalSteps = _files.Length;
            _text = textToFind;
            _path = path;
            _errors = new List<string>();
            _abort = false;
            _mutexAbort = new Mutex();
        }

        protected virtual void OnFileFound(FileFoundEventArgs e)
        {
            if (FileFound != null)
                FileFound(this, e);
        }

        protected virtual void OnAborted(EventArgs e)
        {
            if (Aborted != null)
                Aborted(this, e);
        }

        public override void Start()
        {
            if (!Monitor.TryEnter(this))
                throw new Exception(
                    "Operation is already running");

            _currentStep = 0;
            OnOperationStart(EventArgs.Empty);

            foreach (string file in _files)
            {
                StreamReader sr = null;
                bool found = false;

                try
                {
                    sr = new StreamReader(file);
                    while (!sr.EndOfStream && !found && !AbortRequired)
                    {
                        string line = sr.ReadLine();
                        found = line.Contains(_text);
                    }

                    if (found)
                        OnFileFound(new FileFoundEventArgs(file));

                    if (AbortRequired)
                    {
                        OnAborted(EventArgs.Empty);
                        return;
                    }

                    _currentStep++;
                    OnOperationProgress(EventArgs.Empty);
                }
                catch (ThreadAbortException)
                {
                    OnAborted(EventArgs.Empty);
                    return;
                }
                catch
                {
                    lock (_errors)
                        _errors.Add(file);
                }
                finally
                {
                    if (sr != null)
                        sr.Close();
                }
            }

            Monitor.Exit(this);

            OnOperationEnd(EventArgs.Empty);
        }

        private bool AbortRequired
        {
            get
            {
                _mutexAbort.WaitOne();
                bool b = _abort;
                _mutexAbort.ReleaseMutex();

                return b;
            }
        }

        #region IFindInFilesProgressiveOperation Members

        public event FileFoundEventHandler FileFound;

        public List<string> ErrorsInFiles
        {
            get 
            {
                List<string> lst = null;

                lock (_errors)
                    lst = new List<string>(_errors);

                return lst;
            }
        }

        public string SearchPath
        {
            get { return _path; }
        }

        public string SearchText
        {
            get { return _text; }
        }

        #endregion

        #region IAbortableMembers

        public event EventHandler Aborted;

        public void Abort()
        {
            if (Monitor.TryEnter(this))
            {
                Monitor.Exit(this);
                throw new InvalidOperationException(
                    "Operation is not running");
            }

            _mutexAbort.WaitOne();
            _abort = true;
            _mutexAbort.ReleaseMutex();
        }

        #endregion
    }
}

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