Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Smart Thread Pool

, 27 Aug 2012
A .NET Thread Pool fully implemented in C# with many features.
smartthreadpool.zip
tests
STPTests.dll
bin
Mono
SmartThreadPoolMono.dll
SilverLight
SmartThreadPoolSL.dll
Windows
SmartThreadPool.dll
WindowsCE
SmartThreadPoolCE.dll
demos
Mono
TestSmartThreadPool
SmartThreadPoolMono.dll
TestSmartThreadPoolMono.exe
UsageControl.dll
WorkItemsGroupDemo
SmartThreadPoolMono.dll
UsageControl.dll
WorkItemsGroupDemoMono.exe
SilverLight
SmartThreadPoolSL.dll
STPSLDemo.dll
STPSLDemo.xap
Windows
TestSmartThreadPool
SmartThreadPool.dll
TestSmartThreadPool.exe
UsageControl.dll
WorkItemsGroupDemo
SmartThreadPool.dll
UsageControl.dll
WorkItemsGroupDemo.exe
WindowsCE
SmartThreadPoolCE.dll
STPCEDemo.exe
src
SmartThreadPool
Properties
STPCEDemo
Properties
STPExamples
App.ico
STPSLDemo
Properties
Resources
EmptyCell.bmp
EmptyCell.jpg
GreenCell.bmp
GreenCell.jpg
RedCell.bmp
RedCell.jpg
STPTests
TestSmartThreadPool
App.ico
UsageControl
Properties
WorkItemsGroupDemo
Properties
Settings.settings
smartthreadpool_11.zip
STPTests
TestSmartThreadPool
App.ico
UsageControl
SmartThreadPool
STPExamples
App.ico
smartthreadpool_2.2.3-noexe.zip
STPSLDemo.xap
WindowsPhone
ApplicationIcon.png
Background.png
STPWPDemo.xap
App.ico
EmptyCell.bmp
EmptyCell.jpg
GreenCell.bmp
GreenCell.jpg
RedCell.bmp
RedCell.jpg
STPWPDemo
ApplicationIcon.png
Background.png
Properties
Resources
EmptyCell.jpg
GreenCell.jpg
RedCell.jpg
SplashScreenImage.jpg
App.ico
Settings.settings
smartthreadpool_2.2.3.zip
SmartThreadPool.dll
SmartThreadPoolSL.dll
WindowsPhone
SmartThreadPoolWP.dll
SmartThreadPool.dll
SmartThreadPool.dll
TestSmartThreadPoolMono.exe
UsageControl.dll
SmartThreadPool.dll
UsageControl.dll
WorkItemsGroupDemoMono.exe
SmartThreadPoolSL.dll
STPSLDemo.dll
STPSLDemo.xap
ApplicationIcon.png
Background.png
SmartThreadPoolWP.dll
STPWPDemo.dll
STPWPDemo.xap
SmartThreadPool.dll
TestSmartThreadPool.exe
UsageControl.dll
SmartThreadPool.dll
UsageControl.dll
WorkItemsGroupDemo.exe
App.ico
EmptyCell.bmp
EmptyCell.jpg
GreenCell.bmp
GreenCell.jpg
RedCell.bmp
RedCell.jpg
ApplicationIcon.png
Background.png
EmptyCell.jpg
GreenCell.jpg
RedCell.jpg
SplashScreenImage.jpg
App.ico
Settings.settings
STPTests.dll
smartthreadpool_20.zip
App.ico
App.ico
smartthreadpool_demo.zip
SmartThreadPoolExamples
App.ico
App.ico
smartthreadpool_demo_11.zip
App.ico
App.ico
smartthreadpool_demo_20.zip
App.ico
App.ico
smartthreadpool_src.zip
App.ico
smartthreadpool_src_11.zip
App.ico
smartthreadpool_src_20.zip
App.ico
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)

Share

About the Author

Ami Bar
Software Developer (Senior)
Israel Israel
B.Sc. in Computer Science.
Works as Software Developer.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 27 Aug 2012
Article Copyright 2004 by Ami Bar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid