Skip to main content

Multi-Threading with ThreadPool and a Thread Manager

The past few weeks have been spent trying to make some tasks in a winforms app multi-threaded. Threaded applications can get tricky really fast. I needed to create a thread manager that helped me queue and create new threads via the ThreadPool. Here is the solution I came up with. I by no means think is the best or only solution. It in fact has one constraint with it I will call out at the end. It is an approach to doing threading in .Net applications though.

I needed a class that would do the following:

- Calculate how many threads to spin up

- Calculate how much work to give to each thread

- Queue new threads with the ThreadPool

- Determine if WaitHandles needed to be created for the new threads

- Pass the WaitHandles to the new threads so they can signal when complete

- Return a collection of created WaitHandles back so future process could wait on all threads if needed

First I created a ThreadState object to hold the information I needed to pass to each thread.

   1:  public class ThreadState
   2:  {
   3:       public int UnitId { get; set; }
   4:       public int PageSize { get; set; }
   5:       public int PageNumber { get; set; }
   6:       public WaitHandle WaitHandle { get; set; }
   7:  }

Now the ThreadManager class:

   1:  public WaitHandle[] QueueThreads(double totalCount, ThreadTypes threadType, int UnitID, WaitHandle[] threadsToWaitfor)
   2:          {
   3:              int MaxThreadCount = int.Parse(System.Configuration.ConfigurationManager.AppSettings["MAX_THREADS"]);
   4:              int pageSize = (int)Math.Round(totalCount / MaxThreadCount, 0);
   5:              List<WaitHandle> createdHandles = new List<WaitHandle>();
   7:              //Set the max threads the computer should prep form. This means all additional requests after this number
   8:              // will be queued and wait for previous threads to finish. 
   9:              ThreadPool.SetMaxThreads(MaxThreadCount, MaxThreadCount);
  11:              // This sets the number of idles threads the process should keep ready for additional requests. 
  12:              ThreadPool.SetMinThreads(5,5);
  14:              if (threadsToWaitfor != null && threadsToWaitfor.Count() > 0)
  15:                  WaitHandle.WaitAll(threadsToWaitfor);
  18:              for (int i = 1; i <= MaxThreadCount; i++)
  19:              {
  20:                  // add additional buffer records for final page
  21:                  if (i == MaxThreadCount)
  22:                      pageSize += 10;
  24:                  ThreadState state = new ThreadState()
  25:                  {
  26:                      OrgUnitId = orgUnitID,
  27:                      PageNumber = i,
  28:                      PageSize = pageSize
  29:                  };
  31:                  if (createWaitHandles)
  32:                  {
  33:                      // Each thread should only have 1 handle linked to it that it is responsible for calling single on when it is done.
  34:                      var threadHandle = new AutoResetEvent(false);
  35:                      state.WaitHandle = threadHandle;
  36:                      createdHandles.Add(threadHandle);
  37:                  }
  39:                  switch (threadType)
  40:                  {
  41:                      case ThreadTypes:
  42:                          ThreadPool.QueueUserWorkItem(new ApplicationConverter().ConvertApplications, state);
  43:                          break;
  45:                      default:
  46:                          break;
  47:                  }
  50:              }
  52:              // return an array list for all handles created. This allows other threads to be linked as dependant on these. 
  53:              return createdHandles.ToArray();
  54:          }

Here is the ConvertApplications method:

   1:  public void ConvertApplications(Object state)
   2:  {
   3:     ThreadState tState = (ThreadState)state;
   4:     _pageSize = tState.PageSize;
   5:     _pageNumber = tState.PageNumber;
   6:     _orgUnitId = tState.OrgUnitId;
   7:     DoTheWork();
   9:    // If we have a waithandle it means we need to single when this process has completed
  10:    if (tState.WaitHandle != null)
  11:    {
  12:        ((System.Threading.AutoResetEvent)tState.WaitHandle).Set();
  13:    }
  14:  }

The idea with the ThreadManager class is you can pass in some basic information about the work you want to do and it will create the threads it needs. My work was pulling data from a database with an ORM tool that could handle paging. So the ThreadManager looks at the total count of records to process passed in against the max thread count the app is configured for and calculates how much work each thread needs to do. At line 14 it can stop and wait for other outstanding threads to signal they are done before creating and queuing additional threads.

Since creating AutoResetEvents is heavy on the system there is a flag you can pass in saying rather or not you need threads to single when they are done. This collection of AutoResetEvents is then stored in a collection and returned. This in turn allows your main thread to wait if needed.

This approach works really well, but there is one flaw in it. If you don’t need to spin up and keep track of more then 64 threads this will work fine. If you make the call “WaitHandle.WaitAll()” and pass in an area of WaitHandles that is greater than 64 you will get an error. 64 is the upper cap that WaitHandle.WaitAll can monitor. The reason for this is to try and limit the overhead created for all these WaitHandles. For some situations this may be just the manager you need (or at least a good start on it). For other situation not so much.


Popular posts from this blog

Excel XIRR and C#

I have spend that last couple days trying to figure out how to run and Excel XIRR function in a C# application. This process has been more painful that I thought it would have been when started. To save others (or myself the pain in the future if I have to do it again) I thought I would right a post about this (as post about XIRR in C# have been hard to come by). Lets start with the easy part first. In order to make this call you need to use the Microsoft.Office.Interop.Excel dll. When you use this dll take note of what version of the dll you are using. If you are using a version less then 12 (at the time of this writing 12 was the highest version) you will not have an XIRR function call. This does not mean you cannot still do XIRR though. As of version 12 (a.k.a Office 2007) the XIRR function is a built in function to Excel. Prior version need an add-in to use this function. Even if you have version 12 of the interop though it does not mean you will be able to use the function. The

Experience Profile Anonymous, Unknown and Known contacts

When you first get started with Sitecore's experience profile the reporting for contacts can cause a little confusion. There are 3 terms that are thrown around, 1) Anonymous 2) Unknown 3) Known. When you read the docs they can bleed into each other a little. First, have a read through the Sitecore tracking documentation to get a feel for what Sitecore is trying to do. There are a couple key things here to first understand: Unless you call " IdentifyAs() " for request the contact is always anonymous.  Tracking of anonymous contacts is off by default.  Even if you call "IdentifyAs()" if you don't set facet values for the contact (like first name and email) the contact will still show up in your experience profile as "unknown" (because it has no facet data to display).  Enabled Anonymous contacts Notice in the picture I have two contacts marked in a red box. Those are my "known" contacts that I called "IdentifyAs"

Uniting Testing Expression Predicate with Moq

I recently was setting up a repository in a project with an interface on all repositories that took a predicate. As part of this I needed to mock out this call so I could unit test my code. The vast majority of samples out there for mocking an expression predicate just is It.IsAny<> which is not very helpful as it does not test anything other then verify it got a predicate. What if you actually want to test that you got a certain predicate though? It is actually pretty easy to do but not very straight forward. Here is what you do for the It.IsAny<> approach in case someone is looking for that. this .bindingRepository.Setup(c => c.Get(It.IsAny<Expression<Func<UserBinding, bool >>>())) .Returns( new List<UserBinding>() { defaultBinding }.AsQueryable()); This example just says to always return a collection of UserBindings that contain “defaultBinding” (which is an object I setup previously). Here is what it looks like when you want to pass in an exp

WPF Localization - RESX Option

About a year ago I was building a WPF project in .Net 3.0 and Visual Studio 2005. I wanted to revisit this subject and see what has changed in .Net 3.5 and Visual Studio 2008. I will make a few of these posts to try and cover all the different options (RESX option, LocBaml option, Resource Dictionary Option). In this blog I will focus on using a resx file to localize an application. To show how the resx option is done I created a WPF form with three labels on it. The first label has is text set inline in XAML, the second has it text set via code behind from the resx file and the third has its text set via XAML accessing the resx file. The first thing that needs to happen to setup a project for localization is a small change to the project file. To make this change you will need to open the project file in notepad (or some other generic editor). In the first PropertyGroup section you need to add the follow XML node <UICulture>en-US</UICulture>. So the project file node w

Advanced Item Cloning

Cloning in Sitecore can be extremely useful. It makes reusing of content items and updating of those items very easy. The default capabilities for item cloning can usually handle most needs. The default behavior does have one thing that can really trip you up. By default clone, child items stay linked to the source cloned item and are not reparented to their new cloned parent. The first thing to understand is there are configuration options for cloning that allow you to change how cloning works. The configuration files have them pretty well documented but if you don't know what you are looking for you may not know they are there. <setting name="ItemCloning.Enabled" value="true"/> Specifies whether the Item Cloning feature is enabled Default value on CM and Standalone servers: true. Default value on CD, Processing and Reporting servers: false. <setting name="ItemCloning.NonInheritedFields" value=""/> Specifies a pipe-separated lis