Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

Work Queue based multi-threading

Rate me:
Please Sign up or sign in to vote.
4.96/5 (67 votes)
27 Nov 20046 min read 281.1K   8.7K   237  
Allows an application to queue work that is performed concurrently to the main thread while maintaining exception processing.
#region Copyright (c) 2004 Richard Schneider (Black Hen Limited) 
/*
   Copyright (c) 2004 Richard Schneider (Black Hen Limited) 
   All rights are reserved.

   Permission to use, copy, modify, and distribute this software 
   for any purpose and without any fee is hereby granted, 
   provided this notice is included in its entirety in the 
   documentation and in the source files.
  
   This software and any related documentation is provided "as is" 
   without any warranty of any kind, either express or implied, 
   including, without limitation, the implied warranties of 
   merchantibility or fitness for a particular purpose. The entire 
   risk arising out of use or performance of the software remains 
   with you. 
   
   In no event shall Richard Schneider, Black Hen Limited, or their agents 
   be liable for any cost, loss, or damage incurred due to the 
   use, malfunction, or misuse of the software or the inaccuracy 
   of the documentation associated with the software. 
*/
#endregion

using NUnit.Framework;
using System;
using System.Threading;

namespace BlackHen.Threading.Tests
{
   [TestFixture] public class WorkThreadPoolTests : NUnit.Framework.Assertion
   {
      [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
      public void BadConstruct1()
      {
         WorkThreadPool threadPool = new WorkThreadPool(0, 0);
      }

      [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
      public void BadConstruct2()
      {
         WorkThreadPool threadPool = new WorkThreadPool(4, 3);
      }

      [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
      public void BadConstruct3()
      {
         WorkThreadPool threadPool = new WorkThreadPool(-1, 3);
      }

      [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
      public void BadConstruct4()
      {
         WorkThreadPool threadPool = new WorkThreadPool(4, -1);
      }

      [Test] public void DefaultThreadPool()
      {
         WorkThreadPool threadPool = WorkThreadPool.Default;
         AssertNotNull (threadPool);
      }

      [Test] public void Disposing()
      {
         WorkThreadPool threadPool = new WorkThreadPool();
         threadPool.Dispose();
      }

      private ThreadPriority runningPriority;
      [Test] public void RunningPriority()
      {
         runningPriority = (ThreadPriority) (-1);
         WorkQueue worklist = new WorkQueue();
         worklist.RunningWorkItem += new WorkItemEventHandler(worklist_RunningWorkItem);
         worklist.Add(new WorkQueueTest.SimpleWork(1, ThreadPriority.AboveNormal));
         worklist.WaitAll();

         AssertEquals ("running priority", ThreadPriority.AboveNormal, runningPriority);
      }

      private void worklist_RunningWorkItem(object sender, WorkItemEventArgs e)
      {
         runningPriority = Thread.CurrentThread.Priority;
      }

      private volatile ResourceExceptionEventArgs wteArgs;
      [Test] public void WorkThreadException()
      {
         wteArgs = null;
         IWorkItem work = new FailedWork();
         work.State = WorkItemState.Scheduled;

         WorkThreadPool.Default.ThreadException += new ResourceExceptionEventHandler(OnWorkThreadException);
         WorkThreadPool.Default.BeginWork(work);

         while (wteArgs == null)
         {
            Thread.Sleep(TimeSpan.FromMilliseconds(50));
         }

         AssertNotNull ("WorkThreadException event", wteArgs);
         AssertNotNull (wteArgs.Exception);
         AssertEquals ("WorkThreadException testing.", wteArgs.Exception.Message);
      }
      private void OnWorkThreadException(object sender, ResourceExceptionEventArgs e)
      {
         wteArgs = e;
      }

      private class FailedWork : WorkItem
      {
         public override void Perform()
         {
            // Do nothing
         }

         protected override void ValidateStateTransition(WorkItemState current, WorkItemState next)
         {
            base.ValidateStateTransition (current, next);

            if (next == WorkItemState.Completed)
               throw new Exception("WorkThreadException testing.");
         }


      }

   }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
New Zealand New Zealand
I have been involved with computer engineering (both hardware and software) since 1975. During these almost 30 years, I have mainly been associated with start-up companies, except for a 3-year stint at Digital Equipment Corp. and 2 years at Telecom New Zealand Ltd. My positions have included Analyst, Software Engineer, R&D Manager and Director of Research and Development.

Comments and Discussions