多线程实例
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7![]()
8
namespace MultiThread
9
{
10
/// <summary>
11
/// 多线程实例。
12
/// </summary>
13
public class Form1 : System.Windows.Forms.Form
14
{
15
private System.Windows.Forms.Button btnStart;
16
private System.Windows.Forms.Button btnStop;
17
private System.Windows.Forms.Label labThreadThree;
18
private System.Windows.Forms.Label labThreadOne;
19
private System.Windows.Forms.Label labThreadTwo;
20
private System.Windows.Forms.Label labThreadFour;
21
/// <summary>
22
/// 必需的设计器变量。
23
/// </summary>
24
private System.ComponentModel.Container components = null;
25![]()
26
public Form1()
27
{
28
//
29
// Windows 窗体设计器支持所必需的
30
//
31
InitializeComponent();
32![]()
33
//
34
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
35
//
36
}
37![]()
38
/// <summary>
39
/// 清理所有正在使用的资源。
40
/// </summary>
41
protected override void Dispose( bool disposing )
42
{
43
if( disposing )
44
{
45
if (components != null)
46
{
47
components.Dispose();
48
}
49
}
50
base.Dispose( disposing );
51
}
52![]()
53
Windows Form Designer generated code
135![]()
136
/// <summary>
137
/// 应用程序的主入口点。
138
/// </summary>
139
[STAThread]
140
static void Main()
141
{
142
Application.Run(new Form1());
143
}
144![]()
145
// 定义四个线程变量。
146
public System.Threading.Thread m_thdOne;
147
public System.Threading.Thread m_thdTwo;
148
public System.Threading.Thread m_thdThree;
149
public System.Threading.Thread m_thdFour;
150
// 定义四个计算变量。
151
public int m_iOne;
152
public int m_iTwo;
153
public int m_iThree;
154
public int m_iFour;
155
// 定义一个委托来进行辅助线程调用主线程组件的方法。
156
public delegate void ShowData();
157![]()
158
// 定义四个线程函数。
159
public void ThreadOne()
160
{
161
int i;
162
while (true)
163
{
164
m_iOne += 1;
165
for (i=0; i<10000; i++);
166
labThreadOne.Invoke(new ShowData(ShowOne));
167
}
168
}
169
public void ThreadTwo()
170
{
171
int i;
172
while (true)
173
{
174
m_iTwo += 2;
175
for (i=0; i<10000; i++);
176
labThreadTwo.Invoke(new ShowData(ShowTwo));
177
}
178
}
179
public void ThreadThree()
180
{
181
int i;
182
while (true)
183
{
184
for (i=0; i<10000; i++);
185
m_iThree += 3;
186
labThreadThree.Invoke(new ShowData(ShowThree));
187
}
188
}
189
public void ThreadFour()
190
{
191
int i;
192
while (true)
193
{
194
for (i=0; i<10000; i++);
195
m_iFour += 4;
196
labThreadFour.Invoke(new ShowData(ShowFour));
197
}
198
}
199
// 四个显示线程计算结果的函数。
200
public void ShowOne()
201
{
202
labThreadOne.Text = m_iOne.ToString();
203
}
204
public void ShowTwo()
205
{
206
labThreadTwo.Text = m_iTwo.ToString();
207
}
208
public void ShowThree()
209
{
210
labThreadThree.Text = m_iThree.ToString();
211
}
212
public void ShowFour()
213
{
214
labThreadFour.Text = m_iFour.ToString();
215
}
216
// 开始多线程计算。
217
private void btnStart_Click(object sender, System.EventArgs e)
218
{
219
btnStart.Enabled = false;
220
btnStop.Enabled = true;
221
m_iOne = 0;
222
m_iTwo = 0;
223
m_iThree = 0;
224
m_iFour = 0;
225
m_thdOne = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadOne));
226
m_thdTwo = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadTwo));
227
m_thdThree = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadThree));
228
m_thdFour = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadFour));
229
m_thdOne.Start();
230
m_thdTwo.Start();
231
m_thdThree.Start();
232
m_thdFour.Start();
233
}
234
// 停止多线程计算。
235
private void btnStop_Click(object sender, System.EventArgs e)
236
{
237
btnStart.Enabled = true;
238
btnStop.Enabled = false;
239
m_thdOne.Abort();
240
m_thdTwo.Abort();
241
m_thdThree.Abort();
242
m_thdFour.Abort();
243
}
244
}
245
}
246![]()
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Data;7

8
namespace MultiThread9
{10
/// <summary>11
/// 多线程实例。12
/// </summary>13
public class Form1 : System.Windows.Forms.Form14
{15
private System.Windows.Forms.Button btnStart;16
private System.Windows.Forms.Button btnStop;17
private System.Windows.Forms.Label labThreadThree;18
private System.Windows.Forms.Label labThreadOne;19
private System.Windows.Forms.Label labThreadTwo;20
private System.Windows.Forms.Label labThreadFour;21
/// <summary>22
/// 必需的设计器变量。23
/// </summary>24
private System.ComponentModel.Container components = null;25

26
public Form1()27
{28
//29
// Windows 窗体设计器支持所必需的30
//31
InitializeComponent();32

33
//34
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码35
//36
}37

38
/// <summary>39
/// 清理所有正在使用的资源。40
/// </summary>41
protected override void Dispose( bool disposing )42
{43
if( disposing )44
{45
if (components != null)46
{47
components.Dispose();48
}49
}50
base.Dispose( disposing );51
}52

53
Windows Form Designer generated code135

136
/// <summary>137
/// 应用程序的主入口点。138
/// </summary>139
[STAThread]140
static void Main()141
{142
Application.Run(new Form1());143
}144

145
// 定义四个线程变量。146
public System.Threading.Thread m_thdOne;147
public System.Threading.Thread m_thdTwo;148
public System.Threading.Thread m_thdThree;149
public System.Threading.Thread m_thdFour;150
// 定义四个计算变量。151
public int m_iOne;152
public int m_iTwo;153
public int m_iThree;154
public int m_iFour;155
// 定义一个委托来进行辅助线程调用主线程组件的方法。156
public delegate void ShowData();157

158
// 定义四个线程函数。159
public void ThreadOne()160
{161
int i;162
while (true)163
{164
m_iOne += 1;165
for (i=0; i<10000; i++);166
labThreadOne.Invoke(new ShowData(ShowOne));167
}168
}169
public void ThreadTwo()170
{171
int i;172
while (true)173
{174
m_iTwo += 2;175
for (i=0; i<10000; i++);176
labThreadTwo.Invoke(new ShowData(ShowTwo));177
}178
}179
public void ThreadThree()180
{181
int i;182
while (true)183
{184
for (i=0; i<10000; i++);185
m_iThree += 3;186
labThreadThree.Invoke(new ShowData(ShowThree));187
}188
}189
public void ThreadFour()190
{191
int i;192
while (true)193
{194
for (i=0; i<10000; i++);195
m_iFour += 4;196
labThreadFour.Invoke(new ShowData(ShowFour));197
}198
}199
// 四个显示线程计算结果的函数。200
public void ShowOne()201
{202
labThreadOne.Text = m_iOne.ToString();203
}204
public void ShowTwo()205
{206
labThreadTwo.Text = m_iTwo.ToString();207
}208
public void ShowThree()209
{210
labThreadThree.Text = m_iThree.ToString();211
}212
public void ShowFour()213
{214
labThreadFour.Text = m_iFour.ToString();215
}216
// 开始多线程计算。217
private void btnStart_Click(object sender, System.EventArgs e)218
{219
btnStart.Enabled = false;220
btnStop.Enabled = true;221
m_iOne = 0;222
m_iTwo = 0;223
m_iThree = 0;224
m_iFour = 0;225
m_thdOne = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadOne));226
m_thdTwo = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadTwo));227
m_thdThree = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadThree));228
m_thdFour = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadFour));229
m_thdOne.Start();230
m_thdTwo.Start();231
m_thdThree.Start();232
m_thdFour.Start();233
}234
// 停止多线程计算。235
private void btnStop_Click(object sender, System.EventArgs e)236
{237
btnStart.Enabled = true;238
btnStop.Enabled = false;239
m_thdOne.Abort();240
m_thdTwo.Abort();241
m_thdThree.Abort();242
m_thdFour.Abort();243
}244
}245
}246



浙公网安备 33010602011771号