Foreword: There’s actually no Media Center-specific content in this post, but it’s the logical continuation of the series…
Background Add-In – A Queue-Based Download Manager
Last time, we talked about using a Media Center-hosted WCF service as the basic architecture for the networking browsing and file copying add-in. This time, we’re looking at how that service is actually implemented.
Essentially, it’s an asynchronous design where enqueue/clear requests come in on the main thread, a download queue is processed on a separate thread and then, due to the highly-desirable progress notifications offered by the WebClient class when downloading in asynchronous mode, files are downloaded on yet another thread:
The benefit of having a download queue, as opposed to spawning each new file as a concurrent download (which, incidentally, is what Windows Explorer does) is that it’s easier to track progress and, on low-bandwidth networks such as wi-fi, the batch will actually finish sooner overall. This is especially relevant for a media center PC, which is less likely to be running on ethernet.
The bread and butter of DownloadManager is the DownloadManagerItem class; this represents an item to download, and describes its source, destination and status:
As you can see, it also holds an optional StoredCredential object (which we introduced a few posts ago) which provides the credentials needed to access an item on a password-protected network share.
The DownloadManager class itself uses a Queue<DownloadManagerItem> to hold the downloads; push/pop are controlled using the lock statement to ensure thread safety. It uses a secondary collection, List<DownloadManagerItem> to hold downloads once they have been completed. This enables the history feature in the add-in, giving users a way of opening completed files or retrying failed downloads.
If credentials are supplied, the download thread will instantiate a NetworkSession object in order to run the download using those credentials. The DownloadFileAsync() method of the WebClient class will quite happily accept a UNC path, raising the DownloadProgressChanged event periodically.
To further support the interactive portion of the add-in, the download manager will raise a notification for a short period of time after an item is enqueued; this is achieved simply by having a NotificationVisible property which is set to true during enqueue and automatically resets itself after a few seconds. Similarly, for binding to UI elements, the download manager offers the IsDownloading, CurrentItem and CurrentFileProgress properties.
The entry point for the background add-in uses a ManualResetEvent to ensure that the main thread is blocked for as long as the service is needed.
This series on Media Center application development is nearly over! Next time, we’ll be looking at the implementation of the StoredCredential class, and how it uses the built-in Windows cryptography service provider to securely store passwords for connecting to network shares. After that, i’ll be unveiling the completed Media Center add-in and giving it a permanent home in the newly-created “Projects” section of this site. See you then!