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.Threading;

using NUnit.Framework;

using Amib.Threading;

namespace WorkItemsGroupTests
{
	/// <summary>
	/// Summary description for GetResultExample.
	/// </summary>
	[TestFixture]
	[Category("Test WorkItemsGroup GetResult")]
	public class TestGetResult
	{
		/// <summary>
		/// Example of how to queue a work item and then wait infinitely for the result.
		/// </summary>
		[Test]
		public void BlockingCall() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();
			IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

			bool success = false;

			IWorkItemResult wir = 
				workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

			if (!wir.IsCompleted)
			{
				int result = (int)wir.GetResult();
				success = (1 == result);
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue a work item and then wait on a timeout for the result.
		/// </summary>
		[Test]
		public void Timeout() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();
			IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

			bool success = false;

			IWorkItemResult wir = 
				workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

			try
			{
				wir.GetResult(500, true);
			}
			catch (WorkItemTimeoutException)
			{
				success = true;
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to queue a work item and then cancel it while it is in the queue.
		/// </summary>
		[Test]
		public void WorkItemCanceling() 
		{ 
			// Create a SmartThreadPool with only one thread.
			// It just to show how to use the work item canceling feature
			SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 1);
			IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

			bool success = false;

			// Queue a work item that will occupy the thread in the pool
			IWorkItemResult wir1 = 
				workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

			// Queue another work item that will wait for the first to complete
			IWorkItemResult wir2 = 
				workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

			// Wait a while for the thread pool to start executing the first work item
			Thread.Sleep(100);

			// The first work item cannot be canceled since it is currently executing
			if (!wir1.Cancel())
			{
				// Cancel the second work item while it still in the queue
				if (wir2.Cancel())
				{
					try
					{
						// Retreiving result of a canceled work item throws an exception
						wir2.GetResult();
					}
					catch (WorkItemCancelException)
					{
						success = true;
					}
				}
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

		/// <summary>
		/// Example of how to interrupt the waiting for a work item to complete.
		/// </summary>
		[Test]
		public void WorkItemWaitCanceling() 
		{ 
			SmartThreadPool smartThreadPool = new SmartThreadPool();
			IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);

			ManualResetEvent cancelWaitHandle = new ManualResetEvent(false);

			bool success = false;

			// Queue a work item that will occupy the thread in the pool
			IWorkItemResult wir1 = 
				workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

			// Queue another work item that will wait for the first to complete
			IWorkItemResult wir2 = 
				workItemsGroup.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle);

			try
			{
				wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle);
			}
			catch (WorkItemTimeoutException) 
			{
				success = true;
			}

			smartThreadPool.Shutdown();

			Assert.IsTrue(success);
		} 

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

		private object SignalCancel(object state)
		{ 
			ManualResetEvent cancelWaitHandle = state as ManualResetEvent;
			Thread.Sleep(250);
			cancelWaitHandle.Set();
			return null;
		}
	}
}

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)

About the Author

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

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