Click here to Skip to main content
15,892,768 members
Articles / General Programming / Threads

Smart Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.96/5 (314 votes)
27 Aug 2012Ms-PL40 min read 2.2M   29.1K   1.1K  
A .NET Thread Pool fully implemented in C# with many features.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace STPWPDemo
{
    public partial class UsageControl : UserControl
    {
        private int _rows = 30;
        private int _columns = 2;

        private int _max = 100;	// Maximum value for progress range
        private int _value1 = 30;	// Current progress
        private int _value2 = 60;	// Current progress


        public int Value1
        {
            get { return _value1; }
            set
            {
                _value1 = value;
                UpdateDisplay();
            }
        }

        public int Value2
        {
            get { return _value2; }
            set
            {
                _value2 = value;
                UpdateDisplay();
            }
        }

        public int Maximum
        {
            get { return _max; }
            set
            {
                _max = value;
                UpdateDisplay();
            }
        }


        public UsageControl()
        {
            InitializeComponent();
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutRoot.Height = _rows * 4;
            LayoutRoot.Width = _columns * 18;

            LayoutRoot.RowDefinitions.Clear();

            for (int i = 0; i < _rows; i++)
            {
                LayoutRoot.RowDefinitions.Add(new RowDefinition());
            }

            UpdateDisplay();
        }

        private void UpdateDisplay()
        {
            LayoutRoot.Children.Clear();

            for (int j = 0; j < _columns; j++)
            {
                for (int i = 0; i < _rows; i++)
                {
                    Brush brush = EmptyCell;

                    int percent = i * _max / _rows;

                    if (percent < _value2)
                    {
                        brush = RedCell;
                    }

                    if (percent < _value1)
                    {
                        brush = GreenCell;
                    }

                    Border border = new Border { Background = brush };

                    Grid.SetRow(border, _rows - i - 1);
                    Grid.SetColumn(border, j);
                    LayoutRoot.Children.Add(border);
                }
            }
        }
    }
}

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 Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Israel Israel
B.Sc. in Computer Science.
Works as Software Developer.

Comments and Discussions