Short question:
I would like to spawn a single background thread that would process work items submitted to a queue (like a threadpool with one thread). Some of the work items are capable of reporting progress, some are not. Which one of the .NET's myriad of multithreading approaches should I use?
Long explanation (to avoid asking about the half which doesn't make any sense):
The main window of my winforms application is split vertically into two halves. The left half contains a treeview with items. When the user double-clicks an item in the treeview, the item is opened on the right half. Almost all objects have a lot of properties, split into several sections (represented by tabs). The loading of these properties takes quite a lot of time, typically around 10s, sometimes more. And more properties are added every once in a while, so the time increases.
Currently my single-threaded design makes the UI non-responsive for this time. Naturally this is undesirable. I'd like to load things part-by-part in background and as soon as a part is loaded make it available for use. For other parts I would display a placeholder tab with a loading animation or something. Also, while some parts are loaded in a single lengthy monolithic operation, others consist of lots of smaller function calls and calculations, and could thus display loading progress. For these parts it would be nice to see the progress (especially if they hang somewhere, which happens).
Note that the data source is not thread-safe, so I cannot load two parts simultaneously.
What approach would be best to implement this behavior? Is there some .NET class that would lift some work off my shoulders, or should I just get down and dirty with Thread
?
A ThreadPool
does work item queue management, but there are no facilities for progress reporting. BackgroundWorker
on the other hand supports progress reports, but it's meant for a single work item. Is there perhaps a combination of both?