updater application block for .Net2.0已经发布了,但是我使用了一下,因为是使用后台更新的缘故,所以速度特别慢。30M的东西差不多要花费30分钟,简直无法忍受,弄了个httpdownloader,在这里共享给大家。
你需要Enterprise Libary 2006
你需要Enterprise Libary 2006
HttpDownloader
1using System;
2using System.IO;
3using System.Net;
4using System.Threading;
5using System.Security.Permissions;
6using Microsoft.ApplicationBlocks.Updater;
7using Microsoft.ApplicationBlocks.Updater.Configuration;
8using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
9namespace x.HttpDownloader
10{
11 /**//// <summary>
12 /// Implements a HTTP downloader for the updater application block V2.0.
13 /// </summary>
14 /// <remarks>
15 /// The <c>HttpDownloader</c> class can be used to download application updates via HTTP. It supports synchronous and asynchronous
16 /// operation as well as progress reporting. In addition, the progress reporting is not at the file level, it is at the byte level.
17 /// </remarks>
18 [ConfigurationElementType(typeof(HttpDownloaderProviderData))]
19 public sealed class HttpDownloader : IDownloader
20 {
21 /**//// <summary>
22 /// The thread to perform the download.
23 /// </summary>
24 private Thread _downloaderThread;
25
26 /**//// <summary>
27 /// Contains configuration settings for the HTTP downloader.
28 /// </summary>
29 //private HttpDownloaderProviderData _configuration;
30
31 /**//// <summary>
32 /// The configuration name for this downloader.
33 /// </summary>
34 private const string CONFIGURATION_NAME = "downloaders";
35
36 /**//// <summary>
37 /// The download provider name.
38 /// </summary>
39 private const string DOWNLOAD_PROVIDER_NAME = "HTTPDownloader";
40
41 /**//// <summary>
42 /// A synchronisation object.
43 /// </summary>
44 private readonly object LOCK = new object();
45
46 /**//// <summary>
47 /// Gets the configuration name for the HTTP downloader.
48 /// </summary>
49 public string ConfigurationName
50 {
51 get
52 {
53 return CONFIGURATION_NAME;
54 }
55 set
56 {
57 //nothing to do
58 }
59 }
60
61 /**//// <summary>
62 /// Initialises the HTTP downloader.
63 /// </summary>
64 //public void Initialize(ConfigurationView configurationView)
65 //{
66 // UpdaterConfigurationView updaterConfigurationView = (UpdaterConfigurationView) configurationView;
67 // _configuration = (HttpDownloaderProviderData) updaterConfigurationView.GetDownloadProviderData(DOWNLOAD_PROVIDER_NAME);
68 //}
69
70 /**//// <summary>
71 /// Downloads the specified task synchronously via HTTP. If <paramref name="maxWaitTime"/> is surpassed prior to the download
72 /// completing, a download error is raised.
73 /// </summary>
74 [FileIOPermission(SecurityAction.Demand)]
75 public void Download(UpdaterTask task, TimeSpan maxWaitTime)
76 {
77 try
78 {
79 OnDownloadStarted(new TaskEventArgs(task));
80 //this object is used to perform the downloading on a separate thread
81 AsyncDownloader downloader = new AsyncDownloader(task, new OnDownloadProgressEventHandler(OnDownloadProgress), new OnDownloadTotalSizeCalculationStartedEventHandler(OnDownloadTotalSizeCalculationStarted), new OnDownloadTotalSizeCalculationProgressEventHandler(OnDownloadTotalSizeCalculationProgress), new OnDownloadTotalSizeCalculationCompletedEventHandler(OnDownloadTotalSizeCalculationCompleted), new OnDownloadCompletedEventHandler(OnDownloadCompleted));
82 CreateDownloaderThread(downloader);
83 _downloaderThread.Start();
84 //DateTime endTime = DateTime.Now + maxWaitTime;
85 double endTime = Environment.TickCount + maxWaitTime.TotalMilliseconds;
86 while ((endTime > Environment.TickCount) && !downloader.IsComplete)
87 {
88 Thread.Sleep(100);
89 }
90
91 if (!downloader.IsComplete)
92 {
93 //abort the thread if it didn't complete
94 _downloaderThread.Abort();
95 throw new ApplicationUpdaterException("Download surpassed time out of " + maxWaitTime);
96 }
97 else if (downloader.Exception != null)
98 {
99 //raise the error event if the downloader thread erred out
100 OnDownloadError(new DownloadTaskErrorEventArgs(task, downloader.Exception));
101 }
102 }
103 catch (Exception e)
104 {
105 OnDownloadError(new DownloadTaskErrorEventArgs(task, e));
106 }
107 }
108
109 /**//// <summary>
110 /// Downloads the specified task asynchronously via HTTP.
111 /// </summary>
112 [FileIOPermission(SecurityAction.Demand)]
113 public void BeginDownload(UpdaterTask task)
114 {
115 try
116 {
117 OnDownloadStarted(new TaskEventArgs(task));
118 //this object is used to perform the downloading on a separate thread
119 AsyncDownloader downloader = new AsyncDownloader(task, new OnDownloadProgressEventHandler(OnDownloadProgress), new OnDownloadTotalSizeCalculationStartedEventHandler(OnDownloadTotalSizeCalculationStarted), new OnDownloadTotalSizeCalculationProgressEventHandler(OnDownloadTotalSizeCalculationProgress), new OnDownloadTotalSizeCalculationCompletedEventHandler(OnDownloadTotalSizeCalculationCompleted), new OnDownloadCompletedEventHandler(OnDownloadCompleted));
120 CreateDownloaderThread(downloader);
121 _downloaderThread.Start();
122 }
123 catch (Exception e)
124 {
125 OnDownloadError(new DownloadTaskErrorEventArgs(task, e));
126 }
127 }
128
129 /**//// <summary>
130 /// Cancels an asynchronous HTTP download operation.
131 /// </summary>
132 public bool CancelDownload(UpdaterTask task)
133 {
134 _downloaderThread.Abort();
135 return true;
136 }
137
138 /**//// <summary>
139 /// Creates the downloader thread.
140 /// </summary>
141 private void CreateDownloaderThread(AsyncDownloader downloader)
142 {
143 _downloaderThread = new Thread(new ThreadStart(downloader.Download));
144 _downloaderThread.Name = "Downloader";
145 _downloaderThread.IsBackground = true;
146 }
147
148 /**//// <summary>
149 /// Delegate for handling the <see cref="DownloadTotalSizeCalculationStarted"/> event.
150 /// </summary>
151 public delegate void DownloadTotalSizeCalculationStartedEventHandler(object sender, TaskEventArgs e);
152
153 /**//// <summary>
154 /// Delegate for handling the <see cref="DownloadTotalSizeCalculationCompleted"/> event.
155 /// </summary>
156 <
1using System;
2using System.IO;
3using System.Net;
4using System.Threading;
5using System.Security.Permissions;
6using Microsoft.ApplicationBlocks.Updater;
7using Microsoft.ApplicationBlocks.Updater.Configuration;
8using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
9namespace x.HttpDownloader
10{
11 /**//// <summary>
12 /// Implements a HTTP downloader for the updater application block V2.0.
13 /// </summary>
14 /// <remarks>
15 /// The <c>HttpDownloader</c> class can be used to download application updates via HTTP. It supports synchronous and asynchronous
16 /// operation as well as progress reporting. In addition, the progress reporting is not at the file level, it is at the byte level.
17 /// </remarks>
18 [ConfigurationElementType(typeof(HttpDownloaderProviderData))]
19 public sealed class HttpDownloader : IDownloader
20 {
21 /**//// <summary>
22 /// The thread to perform the download.
23 /// </summary>
24 private Thread _downloaderThread;
25
26 /**//// <summary>
27 /// Contains configuration settings for the HTTP downloader.
28 /// </summary>
29 //private HttpDownloaderProviderData _configuration;
30
31 /**//// <summary>
32 /// The configuration name for this downloader.
33 /// </summary>
34 private const string CONFIGURATION_NAME = "downloaders";
35
36 /**//// <summary>
37 /// The download provider name.
38 /// </summary>
39 private const string DOWNLOAD_PROVIDER_NAME = "HTTPDownloader";
40
41 /**//// <summary>
42 /// A synchronisation object.
43 /// </summary>
44 private readonly object LOCK = new object();
45
46 /**//// <summary>
47 /// Gets the configuration name for the HTTP downloader.
48 /// </summary>
49 public string ConfigurationName
50 {
51 get
52 {
53 return CONFIGURATION_NAME;
54 }
55 set
56 {
57 //nothing to do
58 }
59 }
60
61 /**//// <summary>
62 /// Initialises the HTTP downloader.
63 /// </summary>
64 //public void Initialize(ConfigurationView configurationView)
65 //{
66 // UpdaterConfigurationView updaterConfigurationView = (UpdaterConfigurationView) configurationView;
67 // _configuration = (HttpDownloaderProviderData) updaterConfigurationView.GetDownloadProviderData(DOWNLOAD_PROVIDER_NAME);
68 //}
69
70 /**//// <summary>
71 /// Downloads the specified task synchronously via HTTP. If <paramref name="maxWaitTime"/> is surpassed prior to the download
72 /// completing, a download error is raised.
73 /// </summary>
74 [FileIOPermission(SecurityAction.Demand)]
75 public void Download(UpdaterTask task, TimeSpan maxWaitTime)
76 {
77 try
78 {
79 OnDownloadStarted(new TaskEventArgs(task));
80 //this object is used to perform the downloading on a separate thread
81 AsyncDownloader downloader = new AsyncDownloader(task, new OnDownloadProgressEventHandler(OnDownloadProgress), new OnDownloadTotalSizeCalculationStartedEventHandler(OnDownloadTotalSizeCalculationStarted), new OnDownloadTotalSizeCalculationProgressEventHandler(OnDownloadTotalSizeCalculationProgress), new OnDownloadTotalSizeCalculationCompletedEventHandler(OnDownloadTotalSizeCalculationCompleted), new OnDownloadCompletedEventHandler(OnDownloadCompleted));
82 CreateDownloaderThread(downloader);
83 _downloaderThread.Start();
84 //DateTime endTime = DateTime.Now + maxWaitTime;
85 double endTime = Environment.TickCount + maxWaitTime.TotalMilliseconds;
86 while ((endTime > Environment.TickCount) && !downloader.IsComplete)
87 {
88 Thread.Sleep(100);
89 }
90
91 if (!downloader.IsComplete)
92 {
93 //abort the thread if it didn't complete
94 _downloaderThread.Abort();
95 throw new ApplicationUpdaterException("Download surpassed time out of " + maxWaitTime);
96 }
97 else if (downloader.Exception != null)
98 {
99 //raise the error event if the downloader thread erred out
100 OnDownloadError(new DownloadTaskErrorEventArgs(task, downloader.Exception));
101 }
102 }
103 catch (Exception e)
104 {
105 OnDownloadError(new DownloadTaskErrorEventArgs(task, e));
106 }
107 }
108
109 /**//// <summary>
110 /// Downloads the specified task asynchronously via HTTP.
111 /// </summary>
112 [FileIOPermission(SecurityAction.Demand)]
113 public void BeginDownload(UpdaterTask task)
114 {
115 try
116 {
117 OnDownloadStarted(new TaskEventArgs(task));
118 //this object is used to perform the downloading on a separate thread
119 AsyncDownloader downloader = new AsyncDownloader(task, new OnDownloadProgressEventHandler(OnDownloadProgress), new OnDownloadTotalSizeCalculationStartedEventHandler(OnDownloadTotalSizeCalculationStarted), new OnDownloadTotalSizeCalculationProgressEventHandler(OnDownloadTotalSizeCalculationProgress), new OnDownloadTotalSizeCalculationCompletedEventHandler(OnDownloadTotalSizeCalculationCompleted), new OnDownloadCompletedEventHandler(OnDownloadCompleted));
120 CreateDownloaderThread(downloader);
121 _downloaderThread.Start();
122 }
123 catch (Exception e)
124 {
125 OnDownloadError(new DownloadTaskErrorEventArgs(task, e));
126 }
127 }
128
129 /**//// <summary>
130 /// Cancels an asynchronous HTTP download operation.
131 /// </summary>
132 public bool CancelDownload(UpdaterTask task)
133 {
134 _downloaderThread.Abort();
135 return true;
136 }
137
138 /**//// <summary>
139 /// Creates the downloader thread.
140 /// </summary>
141 private void CreateDownloaderThread(AsyncDownloader downloader)
142 {
143 _downloaderThread = new Thread(new ThreadStart(downloader.Download));
144 _downloaderThread.Name = "Downloader";
145 _downloaderThread.IsBackground = true;
146 }
147
148 /**//// <summary>
149 /// Delegate for handling the <see cref="DownloadTotalSizeCalculationStarted"/> event.
150 /// </summary>
151 public delegate void DownloadTotalSizeCalculationStartedEventHandler(object sender, TaskEventArgs e);
152
153 /**//// <summary>
154 /// Delegate for handling the <see cref="DownloadTotalSizeCalculationCompleted"/> event.
155 /// </summary>
156 <