Click here to Skip to main content
11,581,081 members (64,300 online)
Click here to Skip to main content
Add your own
alternative version

Smart Thread Pool

, 27 Aug 2012 Ms-PL 1.2M 21.6K 997
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.dll
STPTests.dll
TestSmartThreadPool.exe
UsageControl.dll
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
SmartThreadPool.dll
STPTests.dll
TestSmartThreadPool.exe
UsageControl.dll
App.ico
App.ico
smartthreadpool_demo.zip
SmartThreadPoolExamples
App.ico
App.ico
SmartThreadPool.dll
TestSmartThreadPool.exe
UsageControl.dll
smartthreadpool_demo_11.zip
App.ico
SmartThreadPool.dll
STPTests.dll
TestSmartThreadPool.exe
UsageControl.dll
App.ico
smartthreadpool_demo_20.zip
App.ico
SmartThreadPool.dll
STPTests.dll
TestSmartThreadPool.exe
UsageControl.dll
App.ico
smartthreadpool_src.zip
App.ico
smartthreadpool_src_11.zip
App.ico
smartthreadpool_src_20.zip
App.ico
using System;
using System.Diagnostics;
using System.Threading;

using NUnit.Framework;

using Amib.Threading;

namespace SmartThreadPoolTests
{
	/// <summary>
	/// Summary description for MultipleWorkItemsExample.
	/// </summary>
	[TestFixture]
	[Category("TestMultipleWorkItems")]
	public class TestMultipleWorkItems
	{
		/// <summary>
		/// Example of how to queue several work items and then wait infinitely for 
		/// all of them to complete.
		/// </summary>
		[Test]
		public void WaitAll() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			SmartThreadPool.WaitAll(wirs);

			for(int i = 0; i < wirs.Length; ++i)
			{
				if (!wirs[i].IsCompleted)
				{
					success = false;
					break;
				}
				else
				{
					int result = (int)wirs[i].GetResult();
					if (1 != result)
					{
						success = false;
						break;
					}
				}
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue several work items and then wait infinitely for 
		/// one of them to complete.
		/// 
		/// You can use this technique if you have several work items that return the same 
		/// infomration, but use different method to aquire it. Just execute all of them at
		/// once and wait for the first work item to complete.
		/// 
		/// For example: You need an information about a person and you can query several 
		/// information sites (FBI, CIA, etc.). Query all of them at once and use the first
		/// answer to arrive.
		/// </summary>
		[Test]
		public void WaitAny() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = false;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			int index = SmartThreadPool.WaitAny(wirs);

			if (wirs[index].IsCompleted)
			{
				int result = (int)wirs[index].GetResult();
				if (1 == result)
				{
					success = true;
				}
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue several work items and then wait on a timeout for all
		/// of them to complete.
		/// </summary>
		[Test]
		public void WaitAllWithTimeoutSuccess()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			bool timeout = !SmartThreadPool.WaitAll(wirs, 1500, true);
			success = !timeout;

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue several work items and then wait on a timeout for all
		/// of them to complete.
		/// </summary>
		[Test]
		public void WaitAllWithTimeoutFailure()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			bool timeout = !SmartThreadPool.WaitAll(wirs, 10, true);
			success = timeout;

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue several work items and then wait on a timeout for any
		/// of them to complete.
		/// </summary>
		[Test]
		public void WaitAnyWithTimeoutSuccess()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			int index = SmartThreadPool.WaitAny(wirs, 1500, true);

			success = (index != WaitHandle.WaitTimeout);

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		}

		/// <summary>
		/// Example of how to queue several work items and then wait on a timeout for any
		/// of them to complete.
		/// </summary>
		[Test]
		public void WaitAnyWithTimeoutFailure()
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();

			bool success = true;

			IWorkItemResult [] wirs = new IWorkItemResult[5];

			for(int i = 0; i < wirs.Length; ++i)
			{
				wirs[i] = 
					smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
			}

			int index = SmartThreadPool.WaitAny(wirs, 10, true);

			success = (index == WaitHandle.WaitTimeout);

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		}

		[Test]
		public void WaitAllWithEmptyArray()
		{ 

			IWorkItemResult [] wirs = new IWorkItemResult[0];

			bool success = SmartThreadPool.WaitAll(wirs);;

			Assert.IsTrue(success);
		}

		private object DoSomeWork(object state)
		{ 
			Thread.Sleep(1000);
			return 1;
		}
	}
}

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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 27 Aug 2012
Article Copyright 2004 by Ami Bar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid