using System; using System.IO; using System.Threading; using System.Xml.Serialization; using Microsoft.ApplicationBlocks.Updater; using Microsoft.ApplicationBlocks.Updater.Configuration; using Microsoft.Practices.EnterpriseLibrary.Configuration; namespace MyApp.Downloaders { // This is the downloader class public class UNCDownloader : IDownloader { public UNCDownloader() { } private UpdaterConfigurationView updaterConfigurationView; bool isCompleted = false; bool isSuccessful = false; Thread asyncDownloadThread; // Method to perform a synchronous download public void Download(UpdaterTask task, TimeSpan maxWaitTime) { try { OnDownloadStarted(new TaskEventArgs(task)); AsyncDownloader aDownload = new AsyncDownloader(task, new DownloadCompleted(OnDownloadCompleted)); Thread downloadThread = new Thread(new ThreadStart(aDownload.DownloadFiles)); downloadThread.Start(); double endTime = Environment.TickCount + maxWaitTime.TotalMilliseconds; do { if (isCompleted == true) { break; } } while(Environment.TickCount < endTime); // If the file is still not ready, throw a timeout error if(isSuccessful == false) { downloadThread.Abort(); throw new Exception("Time out error"); } } catch (Exception ex) { OnDownloadError(new DownloadTaskErrorEventArgs(task, ex)); } } public void BeginDownload(UpdaterTask task) { try { OnDownloadStarted(new TaskEventArgs(task)); AsyncDownloader aDownload = new AsyncDownloader(task, new DownloadCompleted(OnDownloadCompleted)); asyncDownloadThread = new Thread(new ThreadStart(aDownload.DownloadFiles)); asyncDownloadThread.Start(); } catch (Exception ex) { OnDownloadError(new DownloadTaskErrorEventArgs(task, ex)); } } public bool CancelDownload(UpdaterTask task) { asyncDownloadThread.Abort(); return true; } public event DownloadTaskStartedEventHandler DownloadStarted; public event DownloadTaskProgressEventHandler DownloadProgress; public event DownloadTaskErrorEventHandler DownloadError; public event DownloadTaskCompletedEventHandler DownloadCompleted; private void OnDownloadStarted( TaskEventArgs e ) { if ( DownloadStarted != null ) { DownloadStarted( this, e ); } } private void OnDownloadProgress( DownloadTaskProgressEventArgs e ) { if ( DownloadProgress != null ) { DownloadProgress( this, e ); } } private void OnDownloadError( DownloadTaskErrorEventArgs e ) { if ( DownloadError != null ) { DownloadError( this, e ); isSuccessful = false; } } private void OnDownloadCompleted(TaskEventArgs e) { if (DownloadCompleted != null) { DownloadCompleted(this, e); isCompleted = true; isSuccessful = true; } } public string ConfigurationName { get { return "downloader"; } set { } } public void Initialize(ConfigurationView configurationView) { updaterConfigurationView = (UpdaterConfigurationView)configurationView; // No downloader specific configuration required for UNCDownloader, so the // updaterConfigurationView object is not used. // If it is required, use the // updaterConfigurationView.GetDownloadProviderData method. // to access the data. } } [XmlRoot("downloader", Namespace=ApplicationUpdaterSettings.ConfigurationNamespace )] public sealed class UNCDownloaderProviderData : DownloadProviderData { public UNCDownloaderProviderData() { } public override string TypeName { get { return typeof( UNCDownloader ).AssemblyQualifiedName; } set {} } } // Delegate for "download completed" event internal delegate void DownloadCompleted(TaskEventArgs e); class AsyncDownloader { internal AsyncDownloader(UpdaterTask task, DownloadCompleted callbackFunction) { _task = task; _callbackFunction = callbackFunction; } private UpdaterTask _task; private DownloadCompleted _callbackFunction; public event DownloadTaskProgressEventHandler DownloadProgress; private void OnDownloadProgress( DownloadTaskProgressEventArgs e ) { if ( DownloadProgress != null ) { DownloadProgress( this, e ); } } internal void DownloadFiles() { for(int i = 0; i<_task.Manifest.Files.Count; i++) { string srcFile = _task.Manifest.Files.Base + _task.Manifest.Files[i].Source; //string destFile = Path.Combine(_task.DownloadFilesBase, _task.Manifest.Files[i].Source); string destFile = Path.Combine(_task.Manifest.Application.Location, _task.Manifest.Files[i].Source); if ( !Directory.Exists( Path.GetDirectoryName( destFile ) ) ) { Directory.CreateDirectory( Path.GetDirectoryName( destFile ) ); } FileInfo src = new FileInfo(srcFile); src.CopyTo(destFile, true); //Thread.Sleep(TimeSpan.FromMinutes(1)); OnDownloadProgress(new DownloadTaskProgressEventArgs( 0,0,_task.Manifest.Files.Count,i+1,_task)); } _callbackFunction(new TaskEventArgs(_task)); } } }