吉吉于

free

生产者与消费者(C#)

这图片500KB,还凑活。以下程序运行模样如上图。

PS:今天没去图书馆,没去食堂,头有点晕。

还有,上边贴的那首歌怎么样,摇滚,我感觉还不错。

还有,我整天写代码,心仍感不足,仍愧疚,虽然已经到了极限。

累,

持续用声音刺激神经。

001 using System;
002 using System.Collections.Generic;
003 using System.ComponentModel;
004 using System.Data;
005 using System.Drawing;
006 using System.Text;
007 using System.Windows.Forms;
008 using System.Threading;
009
010 namespace Producer_and_Consumer
011 {
012     public partial class Form1 : Form
013     {
014         private int PoolNum = ;//缓冲池数目
015         private int Pnum = ;//生产者数目
016         private int Cnum = ;//消费者数目
017         private bool isPRunning = false;//生产者线程状态
018         private bool isCRunning = false;//消费者线程状态
019         private int outPnum = ;//生产数目
020         private int outCnum = ;//消费数目
021         private int persent = ;//进度条百分比
022
023         private delegate void PDelegate(int num);//生产者委托
024         private delegate void CDelegate(int num);//消费者委托
025
026         public Form1()
027         {
028             InitializeComponent();
029         }
030
031         private void Form1_Load(object sender, EventArgs e)
032         {
033             producerText.Text = “10″;//初始化生产者数目
034             consumerText.Text = “4″;//初始化消费者数目
035             stopBtn.Enabled = false;
036         }
037
038         private void button1_Click(object sender, EventArgs e)//开始
039         {
040             stopBtn.Enabled = true;
041             startBtn.Enabled = false;
042             try
043             {
044                 int.Parse(producerText.Text);
045                 int.Parse(consumerText.Text);
046             }
047             catch
048             {
049                 if (MessageBox.Show(“请输入数字”, “错误”, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
050                 {
051                     producerText.Clear();
052                     producerText.Focus();
053                     consumerText.Clear();
054                     startBtn.Enabled = true;
055                 }
056             }
057             if (producerText.Text == “” || consumerText.Text == “”)
058             {
059                 MessageBox.Show(this, “请输入数目”, “错误”);
060             }
061             else
062             {
063                 outPnum = Convert.ToInt32(producerText.Text);//获取生产者数目
064                 outCnum = Convert.ToInt32(consumerText.Text);//获取消费者数目
065             }
066             for (int i = ; i < outPnum; i++)
067             {
068                 Thread pthread = new Thread(PMethod);
069                 isPRunning = true;
070                 pthread.IsBackground = true;
071                 pthread.Start();
072             }
073             for (int j = ; j < outCnum; j++)
074             {
075                 Thread cthread = new Thread(CMethod);
076                 isCRunning = true;
077                 cthread.IsBackground = true;
078                 cthread.Start();
079             }
080
081         }
082         private void PMethod()//生产者方法
083         {
084             while (isPRunning)
085             {
086                 if (persent < 100)
087                 {
088                     Pnum++;
089                     PAdd(Pnum);
090                     Thread.Sleep(1000);
091                 }
092                 else
093                 {
094                     PAdd(Pnum++);
095                     Thread.Sleep(1000);
096                 }
097             }
098         }
099         private void PAdd(int num)
100         {
101             if (producerText.InvokeRequired)
102             {
103                 PDelegate p = new PDelegate(PAdd);
104                 productNum.Invoke(p, num);
105             }
106             else
107             {
108                 productNum.Text = “”;
109                 productNum.AppendText(Convert.ToString(num));
110                 if (outPnum - outCnum > 100)
111                 { PoolNum = 100; }
112                 else if (outPnum - outCnum < )
113                 { PoolNum = ; }
114                 else
115                 { PoolNum = outPnum - outCnum; }
116                 poolCount.Text = Convert.ToString(PoolNum);
117             }
118             try
119             {
120                 persent = (int)(100 * (((float)Pnum - (float)Cnum) / 100.0));
121                 progressBar1.Value = persent;
122                 if (persent >= 100)
123                     label6.Text = “生产者等待中…”;
124                 else
125                     label6.Text = “未等待”;
126             }
127             catch { }
128         }
129         private void CMethod()//消费者方法
130         {
131             while (isCRunning)
132             {
133                 if (persent > )
134                 {
135                     Cnum++;
136                     CAdd(Cnum);
137                     Thread.Sleep(1000);
138                 }
139                 else
140                 {
141                     CAdd(Cnum++);
142                     Thread.Sleep(1000);
143                 }
144             }
145         }
146         private void CAdd(int num)
147         {
148             if ( consumeNum.InvokeRequired)
149             {
150                 CDelegate c = new CDelegate(CAdd);
151                 consumeNum.Invoke(c, num);
152             }
153             else
154             {
155                 consumeNum.Text = “”;
156                 consumeNum.AppendText(Convert.ToString(num));
157                 if (outPnum - outCnum< )
158                 { PoolNum = ; }
159                 else if (outPnum - outCnum >=100)
160                 { PoolNum = 100; }
161                 else
162                 { PoolNum = outPnum - outCnum; }
163                 poolCount.Text = Convert.ToString(PoolNum);
164             }
165             try
166             {
167                 persent = (int)(100 * (((float)Pnum - (float)Cnum) / 100.0));
168                 progressBar1.Value = persent;
169                 if (persent <= )
170                     label7.Text = “消费者等待中…”;
171                 else
172                     label7.Text = “未等待”;
173             }
174             catch { }
175         }
176
177         private void stopBtn_Click(object sender, EventArgs e)
178         {
179             isPRunning = false;
180             isCRunning = false;
181             startBtn.Enabled = true;
182             stopBtn.Enabled = false;
183         }
184
185     }
186 }

转载请注明:于哲的博客 » 生产者与消费者(C#)