ProgressBar因在C#中下载多个文件而波动

时间:2016-06-16 18:20:51

标签: c# multithreading webclient webclient-download

我正在实现一个Windows窗体应用程序,我们将url作为多行文本框的输入,并使用循环我们正在下载文件。在下载功能中,我正在实现一个webclient进行下载。

使用循环会创建太多Webclient请求。现在由于我有一个progressBar,它会导致进度条波动。这是一个程序的图像。

enter image description here

我正在寻找一些方法,以便只下载一个文件并导致其他文件暂停一段时间。这将导致progrressbar仅适用于一个文件。

private void Download_Click(object sender, EventArgs e)
    {
        label1.Text = "Starting Download ... Please Wait ...";


        string[] sep = new string[] { "\r\n" };
        string[] lines = textBox1.Text.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < lines.Length; i++)
        {
            download(lines[i]);
        }

    }


    private  void download(string song)
    {

        string name = song;

        song = query(song);
        using (webclient = new WebClient())
        {
            // ...statements
            using (client = new WebClient())
            {
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFileAsync(new Uri(DOWN_URL + download_url), @dest + "\\" + name + ".mp3");
            }
        }



    }

    private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        count++;
        label1.Text = "Downloaded " + count.ToString() + " song(s)";
    }

    private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {

        progressBar1.Value = e.ProgressPercentage;
        label1.Text = "Downloading";
    }

0 个答案:

没有答案