多线程互斥实例3
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
using System.Threading;
9![]()
10
namespace ThreadMutex3
11
{
12
/// <summary>
13
/// 多线程互斥实例3。
14
/// </summary>
15
public class Form1 : System.Windows.Forms.Form
16
{
17
private System.Windows.Forms.Button button1;
18
private System.Windows.Forms.RichTextBox richTextBox1;
19
/// <summary>
20
/// 必需的设计器变量。
21
/// </summary>
22
private System.ComponentModel.Container components = null;
23![]()
24
public Form1()
25
{
26
//
27
// Windows 窗体设计器支持所必需的
28
//
29
InitializeComponent();
30![]()
31
//
32
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
33
//
34
}
35![]()
36
/// <summary>
37
/// 清理所有正在使用的资源。
38
/// </summary>
39
protected override void Dispose( bool disposing )
40
{
41
if( disposing )
42
{
43
if (components != null)
44
{
45
components.Dispose();
46
}
47
}
48
base.Dispose( disposing );
49
}
50![]()
51
Windows Form Designer generated code
97![]()
98
/// <summary>
99
/// 应用程序的主入口点。
100
/// </summary>
101
[STAThread]
102
static void Main()
103
{
104
Application.Run(new Form1());
105
}
106
// 定义私有变量。
107
public Thread a;
108
public Thread b;
109
// 线程A。
110
public void ThreadA()
111
{
112
while(true)
113
{
114
Monitor.Enter(this);
115
richTextBox1.Text += "A";
116
Monitor.Exit(this);
117
Thread.Sleep(10);
118
}
119
}
120
// 线程B。
121
public void ThreadB()
122
{
123
while(true)
124
{
125
Monitor.Enter(this);
126
richTextBox1.Text += "B";
127
Monitor.Exit(this);
128
Thread.Sleep(20);
129
}
130
}
131
// 开始多线程。
132
private void button1_Click(object sender, System.EventArgs e)
133
{
134
a = new Thread(new ThreadStart(this.ThreadA));
135
b = new Thread(new ThreadStart(this.ThreadB));
136
a.Start();
137
b.Start();
138
button1.Enabled = false;
139
}
140
// 结束多线程。
141
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
142
{
143
a.Abort();
144
b.Abort();
145
}
146
}
147
}
148![]()
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
using System.Threading;9

10
namespace ThreadMutex311
{12
/// <summary>13
/// 多线程互斥实例3。14
/// </summary>15
public class Form1 : System.Windows.Forms.Form16
{17
private System.Windows.Forms.Button button1;18
private System.Windows.Forms.RichTextBox richTextBox1;19
/// <summary>20
/// 必需的设计器变量。21
/// </summary>22
private System.ComponentModel.Container components = null;23

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

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

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

51
Windows Form Designer generated code97

98
/// <summary>99
/// 应用程序的主入口点。100
/// </summary>101
[STAThread]102
static void Main() 103
{104
Application.Run(new Form1());105
}106
// 定义私有变量。107
public Thread a;108
public Thread b;109
// 线程A。110
public void ThreadA()111
{112
while(true)113
{114
Monitor.Enter(this);115
richTextBox1.Text += "A";116
Monitor.Exit(this);117
Thread.Sleep(10);118
}119
}120
// 线程B。121
public void ThreadB()122
{123
while(true)124
{125
Monitor.Enter(this);126
richTextBox1.Text += "B";127
Monitor.Exit(this);128
Thread.Sleep(20);129
}130
}131
// 开始多线程。132
private void button1_Click(object sender, System.EventArgs e)133
{134
a = new Thread(new ThreadStart(this.ThreadA));135
b = new Thread(new ThreadStart(this.ThreadB));136
a.Start();137
b.Start();138
button1.Enabled = false;139
}140
// 结束多线程。141
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)142
{143
a.Abort();144
b.Abort();145
}146
}147
}148



浙公网安备 33010602011771号