I was looking at an old app I wrote in the .NET 2.0 (or maybe 1.1, I can't remember) I've updated it in 2008 with a WPF front end, but haven't done much with the back-end of it which uses some pretty mediocre multi-threading code. (FWIW, the old source code and compiled .exe are available off my blog in this .zip file.)
It was a basic Directory analyzer app I originally wrote as a teaching exercise for new devs to show them multi-threading. (It is just a classic multi-threaded, recursive tree-descent algorithm with the minor "twist" that it generates constant UI progress updates to the UI thread) However, reviewing it recently, two things became apparent:
1) I wasn't perfect in my understanding of multi-threading back then
2) The patterns available in .NET 4.0 to multi-thread have come a long way since then.
3) I could still stand to learn some new "best practices" for multi-threading.
So, I thought I'd post a challenge/conversation starter for guys to re-write the multi-threading bits in a "modern" way. (Either by posting pseudo-code or real code, whatever.) What techniques would you use? Any cool tricks/thoughts on how you would make it better? Places to Lock? Lock on what objects? Best ways to throttle the background stuff so the UI never gets sluggish or non-responsive? Cool ways to use new framework features to make the code more elegant?
General concepts used by code:
It was a basic Directory analyzer app I originally wrote as a teaching exercise for new devs to show them multi-threading. (It is just a classic multi-threaded, recursive tree-descent algorithm with the minor "twist" that it generates constant UI progress updates to the UI thread) However, reviewing it recently, two things became apparent:
1) I wasn't perfect in my understanding of multi-threading back then
2) The patterns available in .NET 4.0 to multi-thread have come a long way since then.
3) I could still stand to learn some new "best practices" for multi-threading.
So, I thought I'd post a challenge/conversation starter for guys to re-write the multi-threading bits in a "modern" way. (Either by posting pseudo-code or real code, whatever.) What techniques would you use? Any cool tricks/thoughts on how you would make it better? Places to Lock? Lock on what objects? Best ways to throttle the background stuff so the UI never gets sluggish or non-responsive? Cool ways to use new framework features to make the code more elegant?
General concepts used by code:
- The XAML/WPF front-end is data-bound to a DirectoryStructure object
- Each DirectorySize object, when instantiated, launches a background process to do the file searching work to determine the size of the files in the directory and generates DirectoryStructure objects for each subdirectory. (Which, in turn generate their own background processes, recursively.)
- Background threads cannot directly interact with the UI thread, or the dataobjects owned by the UI thread. (IOW, UI stuff must be invoked via the Dispatcher object.)
- The algorithm must keep the UI reasonably responsive, and must not cause resource starvation, stack overflows or any other nasty multi-threaded bugs.
- WPF databinding engine requires that the data-objects fire PropertyChanged events to inform the UI of its need to update itself for new values. If PropertyChanged events aren't fired when values change, the UI may not correctly update itself.
- A parent thread will typically finish its work before its children will finish theirs, but the parent's data will not be correct until the children finish their work. Somehow, upon completion of a parent's work, its thread must yield to other threads while waiting for children to complete. (IOW, since the app will probably launch more threads than it can simultaneously work on, you can't have an "active" thread polling for other threads to complete, because the threads that need to complete, quite possibly be waiting for the parent threads to yield before they start.) But, after its children complete, the parent thread must be active long enough to compute its final values and send PropertyChanged events to the UI