VS 2008 SP1DownloadStringAysnc - 不顯示進度
我正在使用DownloadStringAysnc。但是,ProgressChanged事件直到字符串被下載後才顯示進度。
即使當我嘗試下載包含在大文件中的字符串時。程序仍然是響應,所以我知道它正在做一些事情。但是,progressChanged事件觸發完成後,進度已經完成。
我知道這是progressChanged和DownloadStringCompleted之後立即觸發。但是,它們應該是一個暫停,因爲該文件相當大。
這是我目前使用的代碼片段。和下面的輸出。奇怪的是,e.progresspercentage是100%。似乎被召喚了兩次。
非常感謝任何意見,
Output in the progress changed event
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Completed Version userstate: [ Version1 ]
private void UpdateAvailable()
{
WebClient wbCheckUpdates = new WebClient();
wbCheckUpdates.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wbCheckUpdates_DownloadProgressChanged);
wbCheckUpdates.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wbCheckUpdates_DownloadStringCompleted);
DownloadFiles df = new DownloadFiles();
string webServerURL = df.webServerPath;
wbCheckUpdates.DownloadStringAsync(new Uri(Path.Combine(webServerURL, "version.txt")), "Version1");
}
void wbCheckUpdates_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Progress version changed userstate: [ " + e.UserState + " ]");
progressBar1.Value = e.ProgressPercentage;
Console.WriteLine("progressBar1.Value [ " + this.progressBar1.Value + " ]");
}
void wbCheckUpdates_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Console.WriteLine("Completed version userstate: [ " + e.UserState + " ]");
}
===========使用編輯DownloadDataAysnc ===============
wbCheckUpdates.DownloadDataAsync(new Uri(Path.Combine(webServerURL, "version.txt")), "Version1");
void wbCheckUpdates_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
byte[] result = e.Result;
Console.WriteLine("Completed data: [ " + System.Text.ASCIIEncoding.Default.GetString(result) + " ]");
}
的結果是一樣的:
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Completed data: [ 1.0.11 ]
只是好奇:在你的代碼中,你真的有方法簽名從括號中縮進八個空格嗎?每當我看到之前,我認爲這是輸入代碼時出錯,但我發現你到處都有。 – 2009-08-01 13:34:54