[C#] Struggling to use ThreadPool to process a Queue of jobs.
Hi all, I'm trying to make a class that takes requests and processes them on a separate thread. Rather than use threads myself, I figured using the ThreadPool is the far smarter/safer thing to do. However, I have the restriction that these tasks must still be completed in the order they are submitted. This means you can't just throw them at QueueUserWorkItem willy-nilly.
I'm trying to think of a way to essentially do:
- Submit a job to be done - this needs to be FAST as it's coming from the main thread
- If the Class wasn't busy, start it.
- When a job completes, automatically start the next task if any are available. Otherwise, wait for the next time a job is inserted.
The only thing I can think of is somehow tracking if a task is running (boolean flag that's used as a lock?). If it's false when a job is submitted, start one and set it to true. Whilst it's in this mode, jobs should just get added to the queue. When a job is finished, it checks the state of the queue and if there are jobs remaining, it just fires off another one?
I feel like this is a common problem that must have been solved.
Posted Nov 28, 2016, 4:40 PM UTC
Step back for a second and count how many concurrent tasks you actually want to be running simultaneously.
You probably just need a task queue with a single background thread (Threadpool is still good to use) that scans for new work and completes jobs in order.
I only want this code to run a single task at once, regardless of what other threads/main thread is doing. It just needs to accept requests and churn through them one by one.
Then you only need your main thread and a single worker thread. You can still use ThreadPool with a thread count of 1 and QueueUserWorkItem.