I want to write my first real MultiThreaded C# Application. While I used a BackgroundWorker before and know a thing or two about lock(object), I never used the Thread object, Monitor.Enter etc. and I’m completely lost where to start designing the Architecture.
Essentially my program runs in the background. Every 5 Minutes, it checks a web service. If the web service returns data, it creates Jobs out of this data and passes it into a JobQueue. The JobQueue then sequentially works on those jobs – if a new job is added while it still is working on one, it will queue the job. Additionally, there is a Web Server to allow remote access to the program.
The way I see it, I need 4 Threads:
- The Main Thread
- The “5-Minute-Timer” and WebService Thread
- The JobQueue
- The Web Server
Thread 2-4 should be created when the program launches and ended when the program ends, so they only run once.
As said, i don’t really know how the architecture would work on that. What would Thread 1 do? When the MyProgram class is instantiated, should it have a Queue<Job> as a Property? How would I start my Thread? As far as I see, I need to pass in a Function into the Thread – where should that function sit? If I have a class “MyJobQueueThreadClass” that has all the functions for Thread 3, how would that access an Object on the MyProgram class? And if a Thread is just a function, how do I prevent it from ending early? As said, Thread 2 waits 5 Minutes, then executes a series of functions, and restarts the 5 minute timer (Thread.Sleep(300)?) over and over again, until my Program is ended (Call Thread.Abort(Thread2) in the Close/Exit/Destructor of MyProgram?)
Let’s go through it, step by step:
1.
The job queue is a data structure:
If this data structure is accessed by multiple threads, you need to lock it:
Let’s add a method that retrieves a job from the web service and adds it to the queue:
And a method that processes jobs in the queue in a loop:
Let’s run this program:
2.
If you run the program, you’ll notice that a job is added every 5 minutes. But
jobQueue.Dequeue()will throw an InvalidOperationException because the job queue is empty until a job is retrieved.To fix that, we turn the job queue into a blocking queue by using a Semaphore:
3.
If you run the program again, it won’t throw the exception and everything should work fine. But you’ll notice that you have to kill the process because the ProcessJobs-thread never ends. So, how to you end your program?
I recommend you define a special job that indicates the end of job processing:
Then stop the timer and add the special job to the job queue:
I hope this implicitly answers all your questions 🙂
Rules of thumb
Start a thread by specifying a method that has access to all necessary data structures
When accessing data structures from multiple threads, you need to lock the data structures
lockstatement will doWhen multiple threads depend on each other (e.g., a thread waiting for another thread to complete a task) use signals
Do not use Thread.Abort, Thread.Interrupt, Thread.Resume, Thread.Sleep, Thread.Suspend, Monitor.Pulse, Monitor.Wait