winform进度条的实现

1.第一种方法:直接实现

(1)界面

winform进度条的实现

(2)设计一个按钮事件

  1.   private void button1_Click(object sender, EventArgs e)
  2.   {
  3.   progressBar1.Value = 0; //清空进度条
  4.   for (int i = 0; i < 100; i++)
  5.   {
  6.   progressBar1.Value += 1;
  7.   textBox1.AppendText("当前进度:%" + i.ToString() + "\r\n");
  8.   Thread.Sleep(50);
  9.   }
  10.    
  11.   }

结果为:

winform进度条的实现

2.第二种方法:子窗体实现

(1)先定义一个进度条窗体progressForm.cs,添加函数

  1.   public void AddProgress()
  2.   {
  3.   progressBar1.Value++;
  4.   label1.Text = progressBar1.Value.ToString() + "%";
  5.   label1.Refresh();

(2)主窗体添加一个按钮事件

  1.   private void button1_Click(object sender, EventArgs e)
  2.   {
  3.   progressForm progress = new progressForm();
  4.   progress.Show();
  5.   for (int i = 0; i < 100; i++)
  6.   {
  7.   progress.AddProgress();
  8.   Thread.Sleep(50); // 毫秒ms
  9.   }
  10.   progress.Close();
  11.    
  12.   }

结果如下:

winform进度条的实现

 

(3)第三种方法:线程实现

效果如下:

winform进度条的实现

步骤如下:

首先定义一个代理用于更新ProgressBar的值(Value)及在执行方法的时候,返回方法的处理信息。

        private delegate void SetPos(int ipos,string vinfo);//代理

相关实现函数如下

  1.   private void button1_Click(object sender, EventArgs e)
  2.   {
  3.   Thread fThread = new Thread(new ThreadStart(SleepT));
  4.   fThread.Start();
  5.   }
  6.    
  7.   //定义新的线程执行函数
  8.   private void SleepT()
  9.   {
  10.   for (int i = 0; i < 500; i++) //滚动条最大值为500
  11.   {
  12.   System.Threading.Thread.Sleep(10);
  13.   SetTextMsg(100 * i / 500, i.ToString() + "\r\n");
  14.   }
  15.   }
  16.    
  17.   //进度条值更新函数(参数必须跟声明的代理参数一样)
  18.   private void SetTextMsg(int ipos, string vinfo)
  19.   {
  20.   if (this.InvokeRequired) //InvokeRequired属性为真时,说明一个创建它以以外的线程(即SleepT)想访问它
  21.   {
  22.   SetPos setpos = new SetPos(SetTextMsg);
  23.   this.Invoke(setpos, new object[] { ipos, vinfo });//SleepT线程调用本控件Form1中的方法
  24.   }
  25.   else
  26.   {
  27.   this.label1.Text = ipos.ToString() + "/100";
  28.   this.progressBar1.Value = Convert.ToInt32(ipos);
  29.   this.textBox1.AppendText(vinfo);
  30.   }
  31.   }

以上三种情况都会产生程序假死,即在进度条运行时不能点击。

4.第四种方法:BackgroundWorker控件实现,不会假死

BackgroundWorker可在单独的线程上执行操作

步骤如下:

(1)主窗体设计,为主窗体添加一个BackgroundWorker控件和按钮控件

winform进度条的实现

(2)主窗体后台代码Form1.cs

  1.   namespace progress_backgroundworker
  2.   {
  3.   public partial class Form1 : Form
  4.   {
  5.   public Form1()
  6.   {
  7.   InitializeComponent();
  8.   this.backgroundWorker1.WorkerReportsProgress = true; //设置能报告进度更新
  9.   this.backgroundWorker1.WorkerSupportsCancellation = true; //设置支持异步取消
  10.   }
  11.    
  12.   private void button1_Click(object sender, EventArgs e)
  13.   {
  14.   this.backgroundWorker1.RunWorkerAsync(); //运行backgroundWorker组件
  15.   ProgressForm form = new ProgressForm(this.backgroundWorker1); //显示进度条窗体
  16.   form.ShowDialog(this);
  17.   form.Close();
  18.   }
  19.    
  20.   //在另一个线程上开始运行(处理进度条)
  21.   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  22.   {
  23.   BackgroundWorker worker = sender as BackgroundWorker;
  24.   for (int i = 0; i < 100; i++)
  25.   {
  26.   System.Threading.Thread.Sleep(100);
  27.   worker.ReportProgress(i);
  28.   if (worker.CancellationPending) //获取程序是否已请求取消后台操作
  29.   {
  30.   e.Cancel = true;
  31.   break;
  32.   }
  33.   }
  34.   }
  35.    
  36.   private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  37.   {
  38.   if (e.Error != null)
  39.   {
  40.   MessageBox.Show(e.Error.Message);
  41.   }
  42.   else if (e.Cancelled)
  43.   {
  44.   MessageBox.Show("取消");
  45.   }
  46.   else
  47.   {
  48.   MessageBox.Show("完成");
  49.   }
  50.   }
  51.    
  52.    
  53.    
  54.   }
  55.   }

(3)子窗体设计

winform进度条的实现

(4)子窗体代码实现ProgressForm.cs

  1.   namespace progress_backgroundworker
  2.   {
  3.   public partial class ProgressForm : Form
  4.   {
  5.   private BackgroundWorker backgroundWorker1; //ProgressForm窗体事件(进度条窗体)
  6.    
  7.   public ProgressForm(BackgroundWorker bgWork)
  8.   {
  9.   InitializeComponent();
  10.   // add my code
  11.   this.backgroundWorker1 = bgWork;
  12.   //绑定进度条改变事件
  13.   this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
  14.   //绑定后台操作完成,取消,异常时的事件
  15.   this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
  16.   }
  17.    
  18.    
  19.   void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  20.   {
  21.   this.progressBar1.Value = e.ProgressPercentage; //获取异步任务的进度百分比
  22.   }
  23.    
  24.   void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  25.   {
  26.   //this.Close(); //执行完之后,直接关闭页面
  27.   }
  28.    
  29.   //取消
  30.   private void btnCancel_Click(object sender, EventArgs e)
  31.   {
  32.   this.backgroundWorker1.CancelAsync(); //请求取消挂起的后台操作
  33.   this.btnCancel.Enabled = false;
  34.   this.Close();
  35.   }
  36.   }
  37.   }

效果如下

winform进度条的实现

更多内容见参考资料

https://blog.csdn.net/ym296900664/article/details/52192289

https://www.cnblogs.com/xwzLoveCshap/p/9271592.html

圆形进度条实现

https://jingyan.baidu.com/article/eae0782742aa9d1fec54852f.html

 

上一篇:BNB代币源码分析


下一篇:UDP(User Datagram Protocol, 用户数据报协议)及代码实例