DownloadStringAysnc - 不显示进度

时间:2009-08-01 13:16:21

标签: c# webclient

VS 2008 SP1

我正在使用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 ]

1 个答案:

答案 0 :(得分:2)

根据文档,DownloadStringAsync不报告进度。请参阅WebClient.DownloadProgressChanged Event

的文档