C# 进度条工作不正常

C# 进度条工作不正常,c#,C#,我只是尝试用webclient为进度条编写代码 请看我的密码 private void button2_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("Invalid Php Url"); } else if (Uri.IsWellFormedUriString(textBox1.Text

我只是尝试用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");
    }
但这件事并没有引起人们的注意,为什么? 这是因为背景工人还是其他问题

请帮帮我


最好的祝愿,

我认为这是因为更新的进度是在后台线程上调用的,而不是在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");   

}   

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

-准确的 -灵动 -精确的
-适当的

如果您尝试异步下载,您是否应该调用client.DownloadFileAsync()?是的,它现在可以工作,这是因为我调用了DownloadFile(),而不是DownloadFileAsync(),谢谢它现在可以工作,这是因为我调用了DownloadFile(),而不是DownloadFileAsync()谢谢你没有回答这个问题,顺便说一句,它已经回答了。