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>();
   6:   
   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);
  10:   
  11:              // This sets the number of idles threads the process should keep ready for additional requests. 
  12:              ThreadPool.SetMinThreads(5,5);
  13:   
  14:              if (threadsToWaitfor != null && threadsToWaitfor.Count() > 0)
  15:                  WaitHandle.WaitAll(threadsToWaitfor);
  16:   
  17:   
  18:              for (int i = 1; i <= MaxThreadCount; i++)
  19:              {
  20:                  // add additional buffer records for final page
  21:                  if (i == MaxThreadCount)
  22:                      pageSize += 10;
  23:   
  24:                  ThreadState state = new ThreadState()
  25:                  {
  26:                      OrgUnitId = orgUnitID,
  27:                      PageNumber = i,
  28:                      PageSize = pageSize
  29:                  };
  30:   
  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:                  }
  38:   
  39:                  switch (threadType)
  40:                  {
  41:                      case ThreadTypes:
  42:                          ThreadPool.QueueUserWorkItem(new ApplicationConverter().ConvertApplications, state);
  43:                          break;
  44:                     
  45:                      default:
  46:                          break;
  47:                  }
  48:   
  49:   
  50:              }
  51:   
  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();
   8:   
   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.

Comments

Popular posts from this blog

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

Anatomy of Sitecore Business Rule - Macros

In previous posts, we talked about  field syntax and the basic structure of business rules . This time we are going to dive into macros in the business rules. Macros are used as part of the business rule syntax. The syntax looks like this and calls for 4 parameters. [Property to set, Operator/Macro, AdditionalParameters, Display text]. When I first started working with business rules the difference between operator and macro was confusing. To add to this confusion some of the out-of-the-box macros are named with the term "operator" (like ListOperator who's configuration points to a class called ListMacro and the class implements IRuleMacro). Anything under the path /sitecore/system/Settings/Rules/Definitions/Macros should be a macro and should implement IRuleMacro. Macros have the follow characteristics: They inherit the IRuleMacro interface The interface requires this execute method void Execute(XElement element, string name, UrlString parameters, string value)

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"

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

Windows Workflow Unit Testing

I know people have very mixed opinions about Windows Workflow and, to be honest, so do I. Really I am not even sure if it has much of a future given the little attention Microsoft has given it. However, despite all that and rather your like it or not there are times when you may use it and want to unit test it. The question is how? Well there are not a lot of options but there is one, that for me, has proven valuable. People tend to use Windows Workflow in a few different ways, so first let me explain how I have use it most. I have never really used it where I programmatically created and instantiate of my own workflow. For me it has pretty much all been using the Windows Workflow designer and using IIS as my workflow host. Then inside those XAML workflows I have custom activities I create and need to test. Do to this I have found one tool that does this pretty well and pretty easy. Microsoft Activities Unit Testing It is an old framework but it still gets the job done. There is