多线程实例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace DemoTest
  5. {
  6.     class Stepclass
  7.     {
  8.         /// <summary>
  9.         /// 共享资源,对进度条的控制
  10.         /// </summary>
  11.         private int step;
  12.         public int Step
  13.         {
  14.             get
  15.             {
  16.                 lock (this)
  17.                     return step;
  18.             }
  19.             set
  20.             {
  21.                 lock (this)
  22.                 step = value;
  23.             }
  24.         }
  25.     }
  26. }
  27. using System;
  28. using System.Collections.Generic;
  29. using System.ComponentModel;
  30. using System.Data;
  31. using System.Drawing;
  32. using System.Text;
  33. using System.Windows.Forms;
  34. using System.Threading;
  35. namespace DemoTest
  36. {
  37.     public partial class Form1 : Form
  38.     {
  39.         string runtest = string.Empty;
  40.         //处理进度条的委托
  41.         delegate void SetProcess();
  42.         //线程控制
  43.         AutoResetEvent autoResetEvent;
  44.         //共享资源,获得进度条的位置
  45.         Stepclass stepclass;
  46.         //定义2个线程
  47.         Thread thread1;
  48.         Thread thread2;
  49.         /// <summary>
  50.         /// 构造函数
  51.         /// </summary>
  52.         public Form1()
  53.         {
  54.             InitializeComponent();
  55.             autoResetEvent = new AutoResetEvent(false);
  56.             stepclass = new Stepclass();
  57.             Thread.CurrentThread.IsBackground = true;
  58.         }
  59.         /// <summary>
  60.         /// 启动线程1
  61.         /// </summary>
  62.         /// <param name="sender"></param>
  63.         /// <param name="e"></param>
  64.         private void button1_Click(object sender, EventArgs e)
  65.         {
  66.             thread1 = new Thread(new ThreadStart(proc1));
  67.             thread1.Name = "thread1";
  68.             thread1.Start();
  69.         }
  70.         /// <summary>
  71.         /// 线程1处理方法
  72.         /// </summary>
  73.         void proc1()
  74.         {
  75.             if (proBar1.InvokeRequired)
  76.             {
  77.                 for (int i = 1; i <= 5; i++)
  78.                 {
  79.                     Thread.Sleep(500);
  80.                     stepclass.Step += 8;
  81.                     runtest += Thread.CurrentThread.Name + ":"+stepclass.Step.ToString()+"  ";
  82.                     this.BeginInvoke(new SetProcess(proc1));
  83.                     this.BeginInvoke(new setproDel(Set1), new object[] { i * 20 });
  84.                     if (i == 3)
  85.                         autoResetEvent.Set();
  86.                 }
  87.                 MessageBox.Show("thread1 has finished");
  88.             }
  89.             else
  90.             {
  91.                 runtest += DateTime.Now.ToString() + " /n";
  92.                 this.proBarTotal.Value = stepclass.Step;
  93.             }
  94.         }
  95.         public delegate void setproDel(int num);
  96.         void Set1(int num)
  97.         {
  98.             this.proBar1.Value = num;
  99.         }
  100.         void Set2(int num)
  101.         {
  102.             this.proBar2.Value = num;
  103.         }
  104.         void Set3(int num)
  105.         {
  106.             this.proBar3.Value = num;
  107.         }
  108.         /// <summary>
  109.         /// 线程2启动
  110.         /// </summary>
  111.         /// <param name="sender"></param>
  112.         /// <param name="e"></param>
  113.         private void button2_Click(object sender, EventArgs e)
  114.         {
  115.             thread2 = new Thread(new ThreadStart(proc2));
  116.             thread2.Name = "thread2";
  117.             thread2.Start();
  118.         }
  119.         /// <summary>
  120.         /// 线程2处理方法
  121.         /// </summary>
  122.         void proc2()
  123.         {
  124.             if (proBar2.InvokeRequired)
  125.             {
  126.                 for (int i = 1; i <= 10; i++)
  127.                 {
  128.                     Thread.Sleep(500);
  129.                     stepclass.Step += 3;
  130.                     runtest += Thread.CurrentThread.Name + ":" + stepclass.Step.ToString() + "  ";
  131.                     this.BeginInvoke(new SetProcess(proc2));
  132.                     this.BeginInvoke(new setproDel(Set2), new object[] { i * 10 });
  133.                 }
  134.                 MessageBox.Show("thread2 has finished");
  135.             }
  136.             else
  137.             {
  138.                 runtest += DateTime.Now.ToString() + " /n";
  139.                 this.proBarTotal.Value = stepclass.Step;
  140.             }
  141.         }
  142.         
  143.         /// <summary>
  144.         /// 启动线程3
  145.         /// </summary>
  146.         /// <param name="sender"></param>
  147.         /// <param name="e"></param>
  148.         private void button3_Click(object sender, EventArgs e)
  149.         {
  150.             Thread thread3 = new Thread(new ThreadStart(proc3));
  151.             thread3.Name = "thread3";
  152.             thread3.Start();
  153.         }
  154.         void proc3()
  155.         {
  156.             if (proBar3.InvokeRequired)
  157.             {
  158.                 for (int i = 1; i <= 5; i++)
  159.                 {
  160.                     Thread.Sleep(500);
  161.                     stepclass.Step+=6;
  162.                     runtest += Thread.CurrentThread.Name + ":" + stepclass.Step.ToString() + "  ";
  163.                     this.BeginInvoke(new SetProcess(proc3));
  164.                     this.BeginInvoke(new setproDel(Set3), new object[] { i * 20 });
  165.                 }
  166.                 MessageBox.Show("thread3 has finished");
  167.             }
  168.             else
  169.             {
  170.                 runtest += DateTime.Now.ToString() + " /n";
  171.                 this.proBarTotal.Value = stepclass.Step;
  172.             }
  173.         }
  174.         private void button4_Click(object sender, EventArgs e)
  175.         {
  176.             //查看运行过程
  177.             MessageBox.Show(runtest);
  178.         }
  179.     }
  180. }


初始界面

运行界面

点击Runprocess查看运行过程

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值