Implement WebClient’s asynchronous download with cancellation capability

Standard

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

Implement cancellation ability on BackgroundWorker

Standard

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

Easily create a WPF splash screen with status updates via MVVM

Standard

I was in search of an easy implementation of a splash screen for my current project. I wanted to be able to show a splash screen and update it with current status information as my application initialized.

The standard SplashScreen class provided by Microsoft does not support showing dynamic content on the SplashScreen. For this reason, I was not able to use the supplied class. Instead I decided to create my own.

The splash screen I created is simply a regular window bound to a view model. I’ve created a SplashScreenHelper static class that we’ll use to send status updates to the splash screen. Continue reading