2

我只是尝试使用 webclient 为进度条编写代码请参阅我的代码。

 private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            MessageBox.Show("Invalid Php Url");
        }
        else if (Uri.IsWellFormedUriString(textBox1.Text, UriKind.Absolute) == false)
        {
            MessageBox.Show("Invalid Php Url");
        }
        else
        {

            backgroundWorker1.RunWorkerAsync();

        }
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        WebClient client = new WebClient();
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
        client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallBack2);
        client.DownloadFile(textBox1.Text, @"D:\test\test.zip");

    }

    void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
    {
        this.progressBar1.Value = e.ProgressPercentage;
        this.label6.Text = e.ProgressPercentage.ToString();
    }

    void DownloadFileCallBack2(object sender, AsyncCompletedEventArgs c)
    {
        MessageBox.Show("Download Completed");
    }

但是事件不调用为什么?这是因为后台工作人员还是任何其他问题?

请帮我。

此致,

4

2 回答 2

1

我认为这是因为更新的进度是在后台线程而不是 UI 线程上调用的。尝试将 webclient 传递给 DoWork 线程:

WebClient client = new WebClient(); 
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback); 
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallBack2);
backgroundWorker1.RunWorkerAsync(client); 


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)   
{   
    WebClient client = (WebClient)e.Argument;   
    client.DownloadFile(textBox1.Text, @"D:\test\test.zip");   

}   
于 2012-07-27T15:39:10.900 回答
0

您的进度条必须满足以下条件:

-准确 -响应和流畅 -精确 -适当

于 2013-10-09T07:48:38.470 回答