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 ThreadMutex
9

{
10
/**//// <summary>
11
/// 多线程互斥实例。
12
/// </summary>
13
public class Form1 : System.Windows.Forms.Form
14
{
15
private System.Windows.Forms.TextBox textBox1;
16
private System.Windows.Forms.Button button1;
17
private System.Windows.Forms.Button button2;
18
/**//// <summary>
19
/// 必需的设计器变量。
20
/// </summary>
21
private System.ComponentModel.Container components = null;
22
public Form1()
23
{
24
// Windows 窗体设计器支持所必需的
25
InitializeComponent();
26
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
27
}
28
/**//// <summary>
29
/// 清理所有正在使用的资源。
30
/// </summary>
31
protected override void Dispose( bool disposing )
32
{
33
if( disposing )
34
{
35
if (components != null)
36
{
37
components.Dispose();
38
}
39
}
40
base.Dispose( disposing );
41
}
42
43
Windows Form Designer generated code#region Windows Form Designer generated code
44
/**//// <summary>
45
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
46
/// 此方法的内容。
47
/// </summary>
48
private void InitializeComponent()
49
{
50
this.textBox1 = new System.Windows.Forms.TextBox();
51
this.button1 = new System.Windows.Forms.Button();
52
this.button2 = new System.Windows.Forms.Button();
53
this.SuspendLayout();
54
// textBox1
55
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
56
this.textBox1.Multiline = true;
57
this.textBox1.Name = "textBox1";
58
this.textBox1.Size = new System.Drawing.Size(384, 216);
59
this.textBox1.TabIndex = 0;
60
this.textBox1.Text = "";
61
// button1
62
this.button1.Location = new System.Drawing.Point(104, 232);
63
this.button1.Name = "button1";
64
this.button1.TabIndex = 1;
65
this.button1.Text = "开始";
66
this.button1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
67
this.button1.Click += new System.EventHandler(this.button1_Click);
68
// button2
69
this.button2.Enabled = false;
70
this.button2.Location = new System.Drawing.Point(208, 232);
71
this.button2.Name = "button2";
72
this.button2.TabIndex = 2;
73
this.button2.Text = "结束";
74
this.button2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
75
this.button2.Click += new System.EventHandler(this.button2_Click);
76
// Form1
77
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
78
this.ClientSize = new System.Drawing.Size(384, 264);
79
this.Controls.AddRange(new System.Windows.Forms.Control[]
{
80
this.button2,
81
this.button1,
82
this.textBox1});
83
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
84
this.Name = "Form1";
85
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
86
this.Text = "多线程互斥";
87
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
88
this.ResumeLayout(false);
89
90
}
91
#endregion
92
/**//// <summary>
93
/// 应用程序的主入口点。
94
/// </summary>
95
[STAThread]
96
static void Main()
97
{
98
Application.Run(new Form1());
99
}
100
// 定义两个线程变量。
101
System.Threading.Thread thdOne;
102
System.Threading.Thread thdTwo;
103
// 定义一个线程运行状态变量。
104
public const int START = 1;
105
public const int STOP = 2;
106
public int m_iRun = STOP;
107
// 输出显示字符。
108
public void ShowChar(char ch)
109
{
110
lock(this)
111
{
112
textBox1.Text += ch;
113
}
114
}
115
// 第一个线程。
116
public void ThreadOne()
117
{
118
while(true)
119
{
120
System.Threading.Thread.Sleep(50);
121
ShowChar('A');
122
}
123
}
124
// 第二个线程。
125
public void ThreadTwo()
126
{
127
while(true)
128
{
129
System.Threading.Thread.Sleep(25);
130
ShowChar('B');
131
}
132
}
133
// 开始线程运行。
134
private void button1_Click(object sender, System.EventArgs e)
135
{
136
textBox1.Text = "";
137
thdOne = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadOne));
138
thdTwo = new System.Threading.Thread(new System.Threading.ThreadStart(this.ThreadTwo));
139
thdOne.Start();
140
thdTwo.Start();
141
button1.Enabled = false;
142
button2.Enabled = true;
143
m_iRun = START;
144
}
145
// 结束线程运行。
146
private void button2_Click(object sender, System.EventArgs e)
147
{
148
button1.Enabled = true;
149
button2.Enabled = false;
150
m_iRun = STOP;
151
if (thdOne != null)
152
thdOne.Abort();
153
if (thdTwo != null)
154
thdTwo.Abort();
155
}
156
// 关闭窗体。
157
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
158
{
159
if (m_iRun == START)
160
{
161
m_iRun = STOP;
162
if (thdOne != null)
163
thdOne.Abort();
164
if (thdTwo != null)
165
thdTwo.Abort();
166
}
167
}
168
}
169
}
170

2

3

4

5

6

7

8

9



10


11

12

13

14



15

16

17

18


19

20

21

22

23



24

25

26

27

28


29

30

31

32



33

34



35

36



37

38

39

40

41

42

43


44


45

46

47

48

49



50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79



80

81

82

83

84

85

86

87

88

89

90

91

92


93

94

95

96

97



98

99

100

101

102

103

104

105

106

107

108

109



110

111



112

113

114

115

116

117



118

119



120

121

122

123

124

125

126



127

128



129

130

131

132

133

134

135



136

137

138

139

140

141

142

143

144

145

146

147



148

149

150

151

152

153

154

155

156

157

158



159

160



161

162

163

164

165

166

167

168

169

170
