1.使用Timer实现
1.1插入一个timer控件,属性false,interval 100。插入button,插入textbox如下图。
1.2 代码实现
- public void output(string log)
- {
- if (textBox1.GetLineFromCharIndex(textBox1.Text.Length)>100)
- {
- textBox1.Text = "";
- }
- textBox1.AppendText (DateTime.Now.ToString ("HH:mm:ss "+log +"\r\n"));
-
- }
-
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (progressBar1.Value<progressBar1.Maximum)
- {
- progressBar1.Value++;
- output("进度进行中 [" + progressBar1.Value.ToString() + "/" + progressBar1.Maximum + "]....");
- }
- else
- {
- output ("进度已完成!");
- timer1.Enabled = false;
- }
-
- }
- private void button1_Click(object sender, EventArgs e)
- {
- output("进度开始!");
- progressBar1.Value = 0;
- progressBar1.Minimum = 0;
- progressBar1.Maximum = 100;
- timer1.Enabled = true;
- }
1.3 实现效果
2.用Thread和委托实现模拟processBar效果
2.1 用一个label,一个button,一个进度条如下图。
2.2 代码实现
- private void button1_Click(object sender, EventArgs e)
- {
- Worker worker = new Worker();
-
-
- worker.OnStartWorkEvent += new WorkEventHandler(this.OnStartWorkEventHandler);
- worker.OnDoWorkEvent += new WorkEventHandler(this.OnDoWorkEventHandler);
- worker.OnEndWorkEvent += new WorkEventHandler(this.OnEndWorkEventHandler);
-
- Thread thread = new Thread(new ThreadStart(worker.Work));
- thread.Start();
- }
-
-
-
-
-
- void OnStartWorkEventHandler(object sender, WordEventArgs e)
- {
- maxValueDelegate max = new maxValueDelegate(SetMaxValue);
- this.Invoke(max, e);
- }
-
-
-
-
-
-
- void OnDoWorkEventHandler(object sender, WordEventArgs e)
- {
- nowValueDelegate now = new nowValueDelegate(SetNowValue);
- this.Invoke(now, e);
- }
-
-
-
-
-
- void OnEndWorkEventHandler(object sender, WordEventArgs e)
- {
- endValueDelegate end = new endValueDelegate(SetEndValue);
- this.Invoke(end, e);
- }
-
-
-
-
- private void SetMaxValue(WordEventArgs e)
- {
- this.probar_Test.Maximum = e.Position;
- lbl_Info.Text = e.Info;
- }
-
-
-
-
-
- private void SetNowValue(WordEventArgs e)
- {
- this.probar_Test.Value = e.Position;
- lbl_Info.Text = e.Info;
- }
-
-
-
-
-
- private void SetEndValue(WordEventArgs e)
- {
- this.probar_Test.Value = e.Position; ;
- lbl_Info.Text = e.Info;
- MessageBox.Show(e.Info);
-
- }
2.3实现效果
3.总结收获
3.1 实现方法有很多,不怕不知道,就怕不知道
3.2 权衡利弊用最适合你系统的方法
3.3 把功能抽离环境之后,做了好几个小DEMO实现都没有问题,当加入到正在开发的项目中时,就出现了问题。测试环境要真实!开发环境和部署环境要提前做了充分的了解,不要开发了半天才发现框架版本不一致,那就没地方哭了。。