Click here to Skip to main content
15,881,882 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;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace UsageControl
{
	/// <summary>
	/// Summary description for UsageHistoryControl.
	/// </summary>
	public class UsageHistoryControl : System.Windows.Forms.UserControl
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private const int squareWidth = 12;
		private const int maxLastValuesCount = 2000;
		private const int shiftStep = 3;

		private int shift = 0;

		private int [] lastValues1 = new int[maxLastValuesCount];
		private int [] lastValues2 = new int[maxLastValuesCount];
		private int nextValueIndex;
		private int lastValuesCount;

		private int max = 100;
		private int min = 0;

		public UsageHistoryControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			EnableDoubleBuffering();
			Reset();
		}

		public void Reset()
		{
			lastValues1.Initialize();
			lastValues2.Initialize();
			nextValueIndex = 0;
			lastValuesCount = 0;
			Invalidate();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// UsageHistoryControl
			// 
			this.BackColor = System.Drawing.Color.Black;
			this.Name = "UsageHistoryControl";

		}
		#endregion

		private void EnableDoubleBuffering()
		{
			// Set the value of the double-buffering style bits to true.
			this.SetStyle(ControlStyles.DoubleBuffer | 
				ControlStyles.UserPaint | 
				ControlStyles.AllPaintingInWmPaint,
				true);
			this.UpdateStyles();
		}

		protected override void OnResize(EventArgs e)
		{
			// Invalidate the control to get a repaint.
			this.Invalidate();
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			Graphics g = e.Graphics;

			for(int i = 0; i <= ClientRectangle.Width+squareWidth; i += squareWidth)
			{
				g.DrawLine(Pens.Green, i-shift, 0, i-shift, ClientRectangle.Height);
			}

			for(int i = 0; i < ClientRectangle.Height; i += squareWidth)
			{
				g.DrawLine(Pens.Green, 0, i, ClientRectangle.Width, i);
			}

			int startValueIndex = (nextValueIndex-1+maxLastValuesCount)%maxLastValuesCount;

			int prevVal1 = GetRelativeValue(lastValues1[startValueIndex]);
			int prevVal2 = GetRelativeValue(lastValues2[startValueIndex]);

			for(int i = 1; i < lastValuesCount; ++i)
			{
				int index = nextValueIndex - 1 - i;
				if (index < 0)
				{
					index += maxLastValuesCount;
				}

				int val1 = GetRelativeValue(lastValues1[index]);
				int val2 = GetRelativeValue(lastValues2[index]);

				g.DrawLine(
					Pens.Red,
					ClientRectangle.Width-(i-1)*shiftStep, ClientRectangle.Height-prevVal2,
					ClientRectangle.Width-i*shiftStep, ClientRectangle.Height-val2);

				g.DrawLine(
					Pens.LawnGreen,
					ClientRectangle.Width-(i-1)*shiftStep, ClientRectangle.Height-prevVal1,
					ClientRectangle.Width-i*shiftStep, ClientRectangle.Height-val1);

				prevVal1 = val1;
				prevVal2 = val2;
			}
		}

		private int GetRelativeValue(int val)
		{
			int result = val * (ClientRectangle.Height-2) / max + 1;
			return result;
		}

		public void AddValues(int val1, int val2)
		{
			lastValues1[nextValueIndex] = val1;
			lastValues2[nextValueIndex] = val2;
			
			nextValueIndex++;
			nextValueIndex %= maxLastValuesCount;
			lastValuesCount++;
			if (lastValuesCount > maxLastValuesCount)
			{
				lastValuesCount = maxLastValuesCount;
			}
			
			shift += shiftStep;
			shift %= squareWidth;
			Invalidate();
		}

		public int Maximum
		{
			get
			{
				return max;
			}

			set
			{
				// Make sure that the maximum value is never set lower than the minimum value.
				if (value < min)
				{
					min = value;
				}

				max = value;

				// Invalidate the control to get a repaint.
				this.Invalidate();
			}
		}

	}
}

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