When I wrote my article regarding use of the BackgroundWorker to keep the UI responsive, I used the WebClient’s DownloadString method as an example of a long-running process. I used this as an example in explaining how to use the BackgroundWorker, which simply allows for thread-blocking code to be run in a separate thread.
As it turns out, this is a terrible idea. Not the whole thread-blocking code in a separate thread, but the use of a WebClient in a BackgroundWorker. You see, the WebClient class implements a DownloadStringAsync() method. This allows the calling code to continue to run, and the asynchronous process will eventually return a string. But since it’s asynchronous, our UI thread will never get blocked by a long-running process. And since it too supports asynchronous cancellation, there’s no reason to embed it inside a BackgroundWorker. Continue reading
As I mentioned in my article on implementing the BackgroundWorker to keep your WPF user interface responsive, the BackgroundWorker class supports cancellation. I will illustrate here how easy it is to accomplish.
I’ll build this example by continuing the example in the previous article. First, let’s add a BackgroundWorker property to our DataModel class. Continue reading
I recently completed an application that made use of the WebClient object’s DownloadString method to obtain a JSON string. In my development environment retrieval of this data often took upwards of 30 seconds. If you’ve ever incorporated a time-consuming process in your application, you may have noticed that your user interface becomes unresponsive while the process is running.
Why does this happen? Because without special consideration, this time-consuming process is running on the same thread that handles updating of the UI. “How do I keep my UI responsive during a time-consuming process?”, you may ask. This can be easily accomplished through the use of the BackgroundWorker object. In this article, we’ll discuss the BackgroundWorker object, some of its available event handlers, and one of its methods. Continue reading