WebClient异步下载

WebClient异步下载

private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            WebClient wc = new WebClient();
            wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler

(wc_DownloadProgressChanged);
            wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
            wc.DownloadFileAsync(new Uri(textBox1.Text), textBox2.Text);
        }

        void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("下载完成!");
                button1.Text = "完成";
            }
            else
            {
                MessageBox.Show("下载错误:" + e.Error.Message);
            }
            button1.Enabled = true;
        }

        void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            button1.Text = "下载 " + e.ProgressPercentage + "%";
            progressBar1.Value = e.ProgressPercentage;
        }

 

你可能感兴趣的:(client)