Are Background thread really helpful if we're updating our GUI regularly?
I was wondering whether Background thread are really helpful if i need to update my GUI regularly (quite often in my case) ?? I'm working on a WPF (C#) app. While opening an old project file I've done all processing o开发者_如何学Gon background thread, However GUI shows progress and needs to be refreshed regularly.
So does background thread will really help in this case. What if i move this processing to main thread (as ever otherwise i'm not doing anything, just waiting for file to load) ?
Found !! Thanks,
Well, one thing to consider is not updating the progress quite as often. Is the user going to notice the difference between updating it 100 times a second and updating it 10 times a second? You might want to batch the updates. Having said that, unless the updates are actually becoming problematic in terms of performance, I wouldn't take the complexity hit.
However, if you're doing a significant amount of work you should absolutely have it in a background thread. Otherwise your UI will become unresponsive while that work is carrying on - and depending on some details, the progress updates might not even show at all!
Even though your code may not be doing anything else, the UI thread still wants to be able to respond to events - mouse movements etc.
You should definitely watch the WPF 4 PDC note of 2009. It sounds old, but shows several ways of doing "extensive" tasks in several ways (background worker for example).
http://www.microsoftpdc.com/2009/CL10
You should load the file in a other thread and fire events on progress. The mainthread should listen to this events and update the GUI.
You shouldn't use background thread if:
- Your code takes very short period of time to be noticed by user
- Your code takes very long period of time but you don't care about "Not responding" window title and frustrated users.
Speaking seriously they are useful if you want to create responsive user interface. Here is wonderful introductory articles from Lee Campbell: Responsive UIs in WPF - Dispatchers to Concurrency to testability
精彩评论