• backgroundWorker1


     private void button3_Click(object sender, EventArgs e)
            {
                string str = this.textBox2.Text.Trim();
               
               this.backgroundWorker1.RunWorkerAsync(str);
               
            }

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker bw = (BackgroundWorker)sender;

                string url = e.Argument.ToString();

                for (int i = 1; i <= 10; i++)
                {
                    //没有取消后台操作
                    if (!bw.CancellationPending)
                    {
                        WebRequest req = WebRequest.Create(url);
                        WebResponse resp = req.GetResponse();
                        resp.Close();

                        Thread.Sleep(100);
                        bw.ReportProgress(i * 100 / 10, i);
                    }
                }


            }

            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
                label1.Text = e.UserState.ToString() + "/10";
            }

            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                MessageBox.Show("OK");
            }

  • 相关阅读:
    设计模式
    设计模式
    设计模式
    设计模式
    设计模式
    设计模式
    OpenGL学习 (一)
    C#
    Unity
    [开发ing] Unity项目
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/2008809.html
Copyright © 2020-2023  润新知