- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace DemoTest
- {
- class Stepclass
- {
- /// <summary>
- /// 共享资源,对进度条的控制
- /// </summary>
- private int step;
- public int Step
- {
- get
- {
- lock (this)
- return step;
- }
- set
- {
- lock (this)
- step = value;
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace DemoTest
- {
- public partial class Form1 : Form
- {
- string runtest = string.Empty;
- //处理进度条的委托
- delegate void SetProcess();
- //线程控制
- AutoResetEvent autoResetEvent;
- //共享资源,获得进度条的位置
- Stepclass stepclass;
- //定义2个线程
- Thread thread1;
- Thread thread2;
- /// <summary>
- /// 构造函数
- /// </summary>
- public Form1()
- {
- InitializeComponent();
- autoResetEvent = new AutoResetEvent(false);
- stepclass = new Stepclass();
- Thread.CurrentThread.IsBackground = true;
- }
- /// <summary>
- /// 启动线程1
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- thread1 = new Thread(new ThreadStart(proc1));
- thread1.Name = "thread1";
- thread1.Start();
- }
- /// <summary>
- /// 线程1处理方法
- /// </summary>
- void proc1()
- {
- if (proBar1.InvokeRequired)
- {
- for (int i = 1; i <= 5; i++)
- {
- Thread.Sleep(500);
- stepclass.Step += 8;
- runtest += Thread.CurrentThread.Name + ":"+stepclass.Step.ToString()+" ";
- this.BeginInvoke(new SetProcess(proc1));
- this.BeginInvoke(new setproDel(Set1), new object[] { i * 20 });
- if (i == 3)
- autoResetEvent.Set();
- }
- MessageBox.Show("thread1 has finished");
- }
- else
- {
- runtest += DateTime.Now.ToString() + " /n";
- this.proBarTotal.Value = stepclass.Step;
- }
- }
- public delegate void setproDel(int num);
- void Set1(int num)
- {
- this.proBar1.Value = num;
- }
- void Set2(int num)
- {
- this.proBar2.Value = num;
- }
- void Set3(int num)
- {
- this.proBar3.Value = num;
- }
- /// <summary>
- /// 线程2启动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- thread2 = new Thread(new ThreadStart(proc2));
- thread2.Name = "thread2";
- thread2.Start();
- }
- /// <summary>
- /// 线程2处理方法
- /// </summary>
- void proc2()
- {
- if (proBar2.InvokeRequired)
- {
- for (int i = 1; i <= 10; i++)
- {
- Thread.Sleep(500);
- stepclass.Step += 3;
- runtest += Thread.CurrentThread.Name + ":" + stepclass.Step.ToString() + " ";
- this.BeginInvoke(new SetProcess(proc2));
- this.BeginInvoke(new setproDel(Set2), new object[] { i * 10 });
- }
- MessageBox.Show("thread2 has finished");
- }
- else
- {
- runtest += DateTime.Now.ToString() + " /n";
- this.proBarTotal.Value = stepclass.Step;
- }
- }
- /// <summary>
- /// 启动线程3
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button3_Click(object sender, EventArgs e)
- {
- Thread thread3 = new Thread(new ThreadStart(proc3));
- thread3.Name = "thread3";
- thread3.Start();
- }
- void proc3()
- {
- if (proBar3.InvokeRequired)
- {
- for (int i = 1; i <= 5; i++)
- {
- Thread.Sleep(500);
- stepclass.Step+=6;
- runtest += Thread.CurrentThread.Name + ":" + stepclass.Step.ToString() + " ";
- this.BeginInvoke(new SetProcess(proc3));
- this.BeginInvoke(new setproDel(Set3), new object[] { i * 20 });
- }
- MessageBox.Show("thread3 has finished");
- }
- else
- {
- runtest += DateTime.Now.ToString() + " /n";
- this.proBarTotal.Value = stepclass.Step;
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- //查看运行过程
- MessageBox.Show(runtest);
- }
- }
- }
初始界面
运行界面
点击Runprocess查看运行过程