您的位置:首页 > 编程语言 > C#

C# 多线程经典示例 吃苹果

2017-01-10 00:02 369 查看
本文主要讲述了多线程开发中经典示例,通过本示例,可以加深对多线程的理解。
示例概述:

  下面用一个模拟吃苹果的实例,说明C#中多线程的实现方法。要求开发一个程序实现如下情况:一个家庭有三个孩子,爸爸妈妈不断削苹果往盘子里面放,老大、老二、老三不断从盘子里面取苹果吃。盘子的大小有限,最多只能放5个苹果,并且爸妈不能同时往盘子里面放苹果,妈妈具有优先权。三个孩子取苹果时,盘子不能为空,三人不能同时取,老三优先权最高,老大最低。老大吃的最快,取的频率最高,老二次之。
涉及知识点:

线程Thread 创建并控制线程,设置其优先级并获取其状态。

锁 lock 用于实现多线程同步的最直接办法就是加锁,它可以把一段代码定义为互斥段,在一个时刻内只允许一个线程进入执行,而其他线程必须等待。

事件EventHandler 声明一个事件,用于通知界面做改变

设计思路:

Productor 表示生产者,用于削苹果。

Consumer 表示消费者,用于吃苹果。

Dish 盘子,用于装苹果,做为中间类

EatAppleSmp 的BeginEat()方法,表示开始吃苹果,启动线程

效果图

如下【爸爸妈妈削苹果,孩子吃苹果】:

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using DemoSharp.EatApple;
10
11 namespace DemoSharp
12 {
13     /// <summary>
14     /// 页面类
15     /// </summary>
16     public partial class EatAppleForm : Form
17     {
18         private EatAppleSmp m_EatAppleSmp = new EatAppleSmp();
19
20         public EatAppleForm()
21         {
22             InitializeComponent();
23             InitView();
24             m_EatAppleSmp.PutAction += PutActionMethod;
25             m_EatAppleSmp.GetAction += GetActionMethod;
26         }
27
28         /// <summary>
29         /// 初始化GroupBox
30         /// </summary>
31         private void InitView()
32         {
33             this.gbBaba.Controls.Clear();
34             this.gbMama.Controls.Clear();
35             this.gbDage.Controls.Clear();
36             this.gbErdi.Controls.Clear();
37             this.gbSandi.Controls.Clear();
38         }
39
40         /// <summary>
41         /// 启动线程
42         /// </summary>
43         /// <param name="sender"></param>
44         /// <param name="e"></param>
45         private void btnStart_Click(object sender, EventArgs e)
46         {
47             this.m_EatAppleSmp.BeginEat();
48         }
49
50         /// <summary>
51         /// 放苹果事件
52         /// </summary>
53         /// <param name="sender"></param>
54         /// <param name="e"></param>
55         private void PutActionMethod(object sender, EventArgs e)
56         {
57             Productor p = sender as Productor;
58             if (p != null)
59             {
60                 if (p.Name == "Baba")
61                 {
62                     AddItemToGroupBox(this.gbBaba, this.lblBaba);
63                 }
64                 if (p.Name == "Mama")
65                 {
66                     AddItemToGroupBox(this.gbMama, this.lblMama);
67                 }
68             }
69         }
70
71         /// <summary>
72         /// 吃苹果事件
73         /// </summary>
74         /// <param name="sender"></param>
75         /// <param name="e"></param>
76         public void GetActionMethod(object sender, EventArgs e)
77         {
78             Consumer c = sender as Consumer;
79             if (c != null)
80             {
81                 if (c.Name == "Dage")
82                 {
83                     AddItemToGroupBox(this.gbDage, this.lblDage);
84                 }
85                 if (c.Name == "Erdi")
86                 {
87                     AddItemToGroupBox(this.gbErdi, this.lblErdi);
88                 }
89                 if (c.Name == "Sandi")
90                 {
91                     AddItemToGroupBox(this.gbSandi, this.lblSandi);
92                 }
93             }
94         }
95
96         /// <summary>
97         /// 往指定的GroupBox中添加对象
98         /// </summary>
99         /// <param name="gbView"></param>
100         /// <param name="lbl"></param>
101         private void AddItemToGroupBox(GroupBox gbView,Label lbl)
102         {
103             gbView.Invoke(new Action(() =>
104             {
105                 PictureBox p = new PictureBox();
106                 p.Width = 20;
107                 p.Height = 20;
108                 p.Dock = DockStyle.Left;
109                 p.Image = this.imgLst01.Images[0];
110                 p.Margin = new Padding(2);
111                 gbView.Controls.Add(p);
112
113             }));
114             //显示个数
115             lbl.Invoke(new Action(() => {
116                 if (string.IsNullOrEmpty(lbl.Text))
117                 {
118                     lbl.Text = "0";
119                 }
120                 lbl.Text = (int.Parse(lbl.Text) + 1).ToString();
121             }));
122         }
123     }
124 }


View Code

源码下载链接,请点击:

源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: