Click here to Skip to main content
12,511,553 members (48,060 online)
Click here to Skip to main content

Tagged as

Stats

20.3K views
251 downloads
57 bookmarked
Posted

Dealing with Progressive Operations

, 10 May 2010 CPOL
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 Erik.Utilities.Interfaces;
using Erik.Utilities.Bases;

namespace MultipleThreadsSample
{
    class CompositeFindInFilesPO :
        BackgroundCompositePO, IFindInFilesPO
    {
        string _searchText;
        string _searchPath;

        List<string> _errors;

        public CompositeFindInFilesPO(
            List<IFindInFilesPO> operations,
            string searchText, string searchPath)
            : base() 
        {
            _searchText = searchText;
            _searchPath = searchPath;

            _errors = new List<string>();

            _operations = new List<IProgressiveOperation>();
            foreach (IFindInFilesPO op in operations)
            {
                op.FileFound += (sender, e) => OnFileFound(e);

                op.OperationEnd += (sender, e) =>
                    {
                        if (op.ErrorsInFiles.Count != 0)
                        {
                            lock (_errors)
                                _errors.AddRange(op.ErrorsInFiles);
                        }
                    };

                _operations.Add(op);
            }
            
            _totalSteps = _operations.Sum<IProgressiveOperation>(po => po.TotalSteps);
        }

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

        public string SearchText
        {
            get { return _searchText; }
        }

        public string SearchPath
        {
            get { return _searchPath; }
        }

        #region IFindInFilesPO Members

        public event FileFoundEventHandler FileFound;

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

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

                return lst;
            }
        }

        #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

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 10 May 2010
Article Copyright 2010 by _Erik_
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid