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.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)

Share

About the Author

_Erik_
Technical Lead
Spain Spain
No Biography provided

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