Exception on cancel download of custom downloader

Topics: Updater Application Block
Jun 19, 2005 at 9:39 AM
originally posted by: carellotz


I've created my own custom FtpDownloader and I am experiencing problems when integrating the downloader with the rest of the Updater block. My problems center specifically around using the asynchronous mode of operation. When I call BeginDownload, The ApplicationUpdateManager calls the SubmitTaskAsync on the DownloadManager. It gets an instance of my FtpDownloader by using the DownloaderFactory.CreateDownloader method. This returns a new FtpDownloader instance and it invokes the BeginDownload method implemented. I've implemented the BeginDownload method much the same as the UNCDownloader sample code provided by spawning a thread:

mAsyncDownloadThread = new Thread(new ThreadStart(ftpClient.DownloadFiles));

The mAsyncDownloadThread instance reference has class scope so it will be kept alive as long as the instance of the FtpDownloader is still alive.

The user then Cancels the in progress update, which results in the ApplicationUpdateManager calling EndTask on the DownloadManager class. In stead of using the existing instance that is already downloading the files, the DownloadManager again calls the DownloaderFactory.CreateDownloader method which creates another new instance of the FtpDownloader. The problem however is that the mAsyncDownloadThread is null as this is not the same instance that was used in the BeginDownload method. The following code in the
FtpDownloader.CancelDownload therefore falls over as the mAsyncDownloadThread is not set:


What am I doing wrong? Surely the same instance that was used for starting the download needs to be used to cancel the download? I had a quick look at the BitsDownloader and it uses a IBackgroundCopyManager to get the job that is running to cancel, but this seems to me like BITS specific functionality. How do I go about solving the problem?

Carel Lotz