Click here to Skip to main content
15,895,799 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 38.7K   319   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.Threading;
using Erik.Utilities.Bases;
using Erik.Utilities.Interfaces;

namespace Erik.Utilities.Bases
{
    // This class is just to show a tiny implementation
    // using the Framework's ThreadPool. However, it
    // will not perform very well
    public class BackgroundCompositePO_ThreadPool : 
        BaseProgressiveOperation
    {
        Mutex _curStepMutex;
        protected List<IProgressiveOperation> _operations;

        protected BackgroundCompositePO_ThreadPool()
        {
            _curStepMutex = new Mutex(false);
        }

        public BackgroundCompositePO_ThreadPool(
            List<IProgressiveOperation> operations) : this()
        {
            _operations = operations;
            _totalSteps = _operations.Sum<IProgressiveOperation>(po => po.TotalSteps);
        }

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

            _currentStep = 0;
            OnOperationStart(EventArgs.Empty);

            foreach (IProgressiveOperation po in _operations)
            {
                po.OperationProgress +=
                    (sender, e) =>
                    {
                        IncreaseCurrentStep();
                        OnOperationProgress(EventArgs.Empty);
                    };

                ThreadPool.QueueUserWorkItem(new WaitCallback(InitOperation), po);
            }
        }

        void InitOperation(object operation)
        {
            ((IProgressiveOperation)operation).Start();
        }

        protected void IncreaseCurrentStep()
        {
            _curStepMutex.WaitOne();
            _currentStep++;

            if (_currentStep == _totalSteps)
            {
                OnOperationEnd(EventArgs.Empty);
                Monitor.Exit(this);
            }

            _curStepMutex.ReleaseMutex();
        }
    }
}

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