[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

26

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.

1
zeritorOP•

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.

1
GoAheadMakeMySplay•

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.

2
svgwrk•

I wouldn't do this stuff myself, if I were you; the task parallel library is meant for exactly this kind of stuff. It's built on top of that thread pool and using it means someone else will do the hard stuff for you.

https://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx

1
zeritorOP•

I would love to use it but sadly I'm restricted to a lower version of .NET that doesn't have it.

1
svgwrk•

I will light a candle for you. :(

1