Click here to Skip to main content
15,893,381 members
Articles / Desktop Programming / Win32

ThreadPoolComponent: Multithreading Simplified

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
9 Mar 2009CPOL2 min read 17.6K   311   17  
Provides a simple wrapper around the shared ThreadPool. It may be used to divide a task into a set of threads which are executed concurrently.
using System;

namespace ThreadPoolComponent
{
	class Program
	{
		static void Main(string[] args)
		{
			var component = new ThreadPoolComponent();

			component.ThreadStarted += new EventHandler<ThreadEventArgs>(component_ThreadStarted);
			component.AllThreadsFinished += new EventHandler(component_AllThreadsFinished);

			Console.WriteLine("Starting the threads...\n---");

			component.Start();

			do
			{
				System.Threading.Thread.Sleep(10);
			} while (component.IsRunning);

			Console.Write("Press any key... ");
			Console.ReadKey(true);
		}

		static void component_ThreadStarted(object sender, ThreadEventArgs e)
		{
			Console.WriteLine(string.Format("Thread #{0} says hello.", e.ThreadIndex));
		}

		static void component_AllThreadsFinished(object sender, EventArgs e)
		{
			Console.WriteLine("---\nAll threads are finished.");
		}
	}
}

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 Code Project Open License (CPOL)


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions