private void Form1_Load(object sender, EventArgs e) { progressBar1.Location = new System.Drawing.Point(100, 100); progressBar1.Size = new System.Drawing.Size(300, 20); progressBar1.Maximum = 200; progressBar1.Minimum = 1; } private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart((delegate { for (int i = 1; i <= 200; i++) //ProcessBar控件最大值是多少,这里i就要小于等于多少,如果小于它,滚动条将到不了尽头 { progressBar1.Value = i; //将进度条的值定义为i的值,每次循环过来加1 Thread.Sleep(200); //休息200毫秒在往下执行 } }))); thread.Start(); }
添加以下代码,忽略控件多线程的安全机制:
Control.CheckForIllegalCrossThreadCalls = false;