Simple Way To Structure Threads For Control
BackgroundI've previously discussed the differences between the BackgroundWorker and Thread classes, but I figured it would be useful to touch on some code. I'd like to share the pattern I commonly use when creating threads in C# and discuss some of the highlights.The Single ThreadI like to use this design when I have a single thread I need to run and in the context of my object responsible for running the thread, I do mean having a single thread. Of course, you could have your object in control of multiple threads as long as you repeat this design pattern for each of them.Here's the interface that I'll be using for all of the examples: internal interface IThreadRunner { #region Exposed Members void Start(); void Stop(); #endregion }Behold! internal class SingleThreadRunner : IThreadRunner { #region Fields private readonly object _threadLock; private…