实现:
1、计算progressBar运行时间(既进度条开始到结束的时间)
2、利用多线程运行(用这个应该必须用多线程吧,否则没什么实际意义,因为你有进度条必然是要做其他的事情给予一个进度提示)
效果图如下:
代码如下:
引入命名空间:
using System.Threading;
完整代码:
namespace ProgressBar { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DateTime dt; //记时 private void btnStart_Click(object sender, EventArgs e) { Thread myThread = new Thread(DoData); myThread.IsBackground = true; myThread.Start(int.Parse(txtWhileNumber.Text)); //线程开始 dt = DateTime.Now; //开始记录当前时间 } private delegate void DoDataDelegate(object number); /// <summary> /// 进行循环 /// </summary> /// <param name="number"></param> private void DoData(object number) { if (progressBar1.Invokerequired) { DoDataDelegate d = DoData; progressBar1.Invoke(d, number); } else { progressBar1.Maximum = (int)number; for (int i = 0; i < (int)number; i++) { progressBar1.Value = i; Application.DoEvents(); } MessageBox.Show(DateTime.Now.Subtract(dt).ToString()); //循环结束截止时间 } } private void btnClear_Click(object sender, EventArgs e) { progressBar1.Value = 0; } } }