Showing posts from April, 2011

Threading in C# using the ThreadPool

Threading is one of those topics that creates a lot of discussion in programming. Threading in C# is quite easy. However using it correctly, especially when you start doing data access or sharing information across threads it becomes difficult to keep everything in sync. For this example I'm going to focus on creating some basic threads that run and do some basic output to show we have concurrent threads running. You could create a thread and start it, then join, etc. I prefer to let .net handle the thread creation and use the ThreadPool to do all the thread management for us. You can read more about the ThreadPool here: MSDN Site I normally don't post entire files but this post is a bit different. Let's start with the program that is running the threads. We create 20 threads and pass them an index and a ManualResetEvent. This is so we know which thread is exiting and we need to keep track of when the thread is done processing. If we don't care about when