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

Share

About the Author

_Erik_
Technical Lead
Spain Spain
No Biography provided

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