多线程互斥实例
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
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![]()
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 ThreadMutex9
{10
/// <summary>11
/// 多线程互斥实例。12
/// </summary>13
public class Form1 : System.Windows.Forms.Form14
{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 code92
/// <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



浙公网安备 33010602011771号