事件处理方法将在触发事件的线程中运行,就是说处理方法和事件源会在同一线程

这是测试程序界面。
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 ThreadCrossEventHandler11


{12

/**//// <summary>13
/// Form1 的摘要说明。14
/// </summary>15
public class Form1 : System.Windows.Forms.Form16

{17

/**//// <summary>18
/// 必需的设计器变量。19
/// </summary>20
private System.ComponentModel.Container components = null;21
private System.Windows.Forms.Button button1;22
private System.Windows.Forms.Button button2;23
private System.Windows.Forms.RichTextBox richTxtMainThreadEventHandler;24
private System.Windows.Forms.RichTextBox richTxtCallerEventHandler;25

26
private MainThreadEvnetHandler mainThreadEvent; 27
private bool _tigerEvnetInCaller = false;28

29
public Form1()30

{ 31
InitializeComponent();32
}33

34

/**//// <summary>35
/// 清理所有正在使用的资源。36
/// </summary>37
protected override void Dispose( bool disposing )38

{39
if( disposing )40

{41
if (components != null) 42

{43
components.Dispose();44
}45
}46
base.Dispose( disposing );47
}48

49

Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码50

/**//// <summary>51
/// 设计器支持所需的方法 - 不要使用代码编辑器修改52
/// 此方法的内容。53
/// </summary>54
private void InitializeComponent()55

{56
this.button1 = new System.Windows.Forms.Button();57
this.button2 = new System.Windows.Forms.Button();58
this.richTxtMainThreadEventHandler = new System.Windows.Forms.RichTextBox();59
this.richTxtCallerEventHandler = new System.Windows.Forms.RichTextBox();60
this.SuspendLayout();61
// 62
// button163
// 64
this.button1.Location = new System.Drawing.Point(96, 16);65
this.button1.Name = "button1";66
this.button1.Size = new System.Drawing.Size(128, 23);67
this.button1.TabIndex = 0;68
this.button1.Text = "在主线程触发事件";69
this.button1.Click += new System.EventHandler(this.button1_Click);70
// 71
// button272
// 73
this.button2.Location = new System.Drawing.Point(416, 16);74
this.button2.Name = "button2";75
this.button2.Size = new System.Drawing.Size(128, 23);76
this.button2.TabIndex = 1;77
this.button2.Text = "在子线程触发事件";78
this.button2.Click += new System.EventHandler(this.button2_Click);79
// 80
// richTxtMainThreadEventHandler81
// 82
this.richTxtMainThreadEventHandler.Location = new System.Drawing.Point(8, 56);83
this.richTxtMainThreadEventHandler.Name = "richTxtMainThreadEventHandler";84
this.richTxtMainThreadEventHandler.Size = new System.Drawing.Size(296, 256);85
this.richTxtMainThreadEventHandler.TabIndex = 2;86
this.richTxtMainThreadEventHandler.Text = "";87
// 88
// richTxtCallerEventHandler89
// 90
this.richTxtCallerEventHandler.Location = new System.Drawing.Point(312, 56);91
this.richTxtCallerEventHandler.Name = "richTxtCallerEventHandler";92
this.richTxtCallerEventHandler.Size = new System.Drawing.Size(312, 256);93
this.richTxtCallerEventHandler.TabIndex = 3;94
this.richTxtCallerEventHandler.Text = "";95
// 96
// Form197
// 98
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);99
this.ClientSize = new System.Drawing.Size(632, 317);100
this.Controls.Add(this.richTxtCallerEventHandler);101
this.Controls.Add(this.richTxtMainThreadEventHandler);102
this.Controls.Add(this.button2);103
this.Controls.Add(this.button1);104
this.Name = "Form1";105
this.Text = "Form1";106
this.Load += new System.EventHandler(this.Form1_Load);107
this.ResumeLayout(false);108

109
}110
#endregion111

112

/**//// <summary>113
/// 应用程序的主入口点。114
/// </summary>115
[STAThread]116
static void Main() 117

{118
Application.Run(new Form1());119
}120

121
private void Form1_Load(object sender, System.EventArgs e)122

{123
mainThreadEvent += new MainThreadEvnetHandler(this.MainThreadEvnetProcess);124

125
Thread.CurrentThread.Name = "Main";126
Thread caller = new Thread(new System.Threading.ThreadStart(this.ThreadMethod));127
caller.Name = "Caller";128
caller.IsBackground = true;129
caller.Start(); 130
}131

132
private void ThreadMethod()133

{134
WriteMessage(Thread.CurrentThread.Name,135
string.Format("{0} thread is started",Thread.CurrentThread.Name));136
while(true)137

{138
if(_tigerEvnetInCaller)139

{140
TigerEvent();141
_tigerEvnetInCaller = false;142
}143
}144
}145

146
private void MainThreadEvnetProcess(object sender, EventArgs e)147

{148
WriteMessage(Thread.CurrentThread.Name,149
string.Format("Event handler is processed in thread {0}", Thread.CurrentThread.Name));150
}151

152
private void button1_Click(object sender, System.EventArgs e)153

{154
TigerEvent();155
}156

157
private void TigerEvent()158

{159
WriteMessage(Thread.CurrentThread.Name, 160
string.Format("事件在{0}中触发", Thread.CurrentThread.Name));161
mainThreadEvent(this, new EventArgs());162
}163

164
private void WriteMessage(string threadName, string message)165

{166
if(threadName.Equals("Main"))167
richTxtMainThreadEventHandler.AppendText(message + "\r\n");168
else169
richTxtCallerEventHandler.AppendText(message + "\r\n");170
}171

172
private void button2_Click(object sender, System.EventArgs e)173

{174
_tigerEvnetInCaller = true;175
}176
}177

178
public delegate void MainThreadEvnetHandler(object sender, EventArgs e);179
}180

浙公网安备 33010602011771号