MSDN上安全调用线程的例子
这是一个在MSDN中关于安全调用控件线程的例子
这是一个在MSDN中关于安全调用控件线程的例子
1
using System;2
using System.ComponentModel;3
using System.Threading;4
using System.Windows.Forms;5

6
namespace CrossThreadDemo7


{8
public partial class Form1 : Form9

{10
// This delegate enables asynchronous calls for setting11
// the text property on a TextBox control.12
delegate void SetTextCallback(string text);13

14
// This thread is used to demonstrate both thread-safe and15
// unsafe ways to call a Windows Forms control.16
private Thread demoThread = null;17

18
// This BackgroundWorker is used to demonstrate the 19
// preferred way of performing asynchronous operations.20
private BackgroundWorker backgroundWorker1;21

22
private TextBox textBox1;23
private Button setTextUnsafeBtn;24
private Button setTextSafeBtn;25
private Button setTextBackgroundWorkerBtn;26

27
private System.ComponentModel.IContainer components = null;28

29
public Form1()30

{31
InitializeComponent();32
}33

34
protected override void Dispose(bool disposing)35

{36
if (disposing && (components != null))37

{38
components.Dispose();39
}40
base.Dispose(disposing);41
}42

43
// This event handler creates a thread that calls a 44
// Windows Forms control in an unsafe way.45
private void setTextUnsafeBtn_Click(object sender,EventArgs e)46

{47
this.demoThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));48
this.demoThread.Start();49
}50

51
// This method is executed on the worker thread and makes52
// an unsafe call on the TextBox control.53
private void ThreadProcUnsafe()54

{55
this.textBox1.Text = "This text was set unsafely.";56
}57

58
// This event handler creates a thread that calls a 59
// Windows Forms control in a thread-safe way.60
private void setTextSafeBtn_Click(object sender,EventArgs e)61

{62
this.demoThread =new Thread(new ThreadStart(this.ThreadProcSafe));63
this.demoThread.Start();64
}65

66
// This method is executed on the worker thread and makes67
// a thread-safe call on the TextBox control.68
private void ThreadProcSafe()69

{70
this.SetText("This text was set safely.");71
}72

73
// This method demonstrates a pattern for making thread-safe74
// calls on a Windows Forms control. 75
//76
// If the calling thread is different from the thread that77
// created the TextBox control, this method creates a78
// SetTextCallback and calls itself asynchronously using the79
// Invoke method.80
//81
// If the calling thread is the same as the thread that created82
// the TextBox control, the Text property is set directly. 83

84
private void SetText(string text)85

{86
// InvokeRequired required compares the thread ID of the87
// calling thread to the thread ID of the creating thread.88
// If these threads are different, it returns true.89
if (this.textBox1.InvokeRequired)90

{91
SetTextCallback d = new SetTextCallback(SetText);92

this.Invoke(d, new object[]
{ text });93
}94
else95

{96
this.textBox1.Text = text;97
}98
}99

100
// This event handler starts the form's 101
// BackgroundWorker by calling RunWorkerAsync.102
//103
// The Text property of the TextBox control is set104
// when the BackgroundWorker raises the RunWorkerCompleted105
// event.106
private void setTextBackgroundWorkerBtn_Click(object sender,EventArgs e)107

{108
this.backgroundWorker1.RunWorkerAsync();109
}110

111
// This event handler sets the Text property of the TextBox112
// control. It is called on the thread that created the 113
// TextBox control, so the call is thread-safe.114
//115
// BackgroundWorker is the preferred way to perform asynchronous116
// operations.117

118
private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)119

{120
this.textBox1.Text ="This text was set safely by BackgroundWorker.";121
}122

123

Windows Form Designer generated code#region Windows Form Designer generated code124

125
private void InitializeComponent()126

{127
this.textBox1 = new System.Windows.Forms.TextBox();128
this.setTextUnsafeBtn = new System.Windows.Forms.Button();129
this.setTextSafeBtn = new System.Windows.Forms.Button();130
this.setTextBackgroundWorkerBtn = new System.Windows.Forms.Button();131
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();132
this.SuspendLayout();133
// 134
// textBox1135
// 136
this.textBox1.Location = new System.Drawing.Point(12, 12);137
this.textBox1.Name = "textBox1";138
this.textBox1.Size = new System.Drawing.Size(240, 20);139
this.textBox1.TabIndex = 0;140
// 141
// setTextUnsafeBtn142
// 143
this.setTextUnsafeBtn.Location = new System.Drawing.Point(15, 55);144
this.setTextUnsafeBtn.Name = "setTextUnsafeBtn";145
this.setTextUnsafeBtn.TabIndex = 1;146
this.setTextUnsafeBtn.Text = "Unsafe Call";147
this.setTextUnsafeBtn.Click += new System.EventHandler(this.setTextUnsafeBtn_Click);148
// 149
// setTextSafeBtn150
// 151
this.setTextSafeBtn.Location = new System.Drawing.Point(96, 55);152
this.setTextSafeBtn.Name = "setTextSafeBtn";153
this.setTextSafeBtn.TabIndex = 2;154
this.setTextSafeBtn.Text = "Safe Call";155
this.setTextSafeBtn.Click += new System.EventHandler(this.setTextSafeBtn_Click);156
// 157
// setTextBackgroundWorkerBtn158
// 159
this.setTextBackgroundWorkerBtn.Location = new System.Drawing.Point(177, 55);160
this.setTextBackgroundWorkerBtn.Name = "setTextBackgroundWorkerBtn";161
this.setTextBackgroundWorkerBtn.TabIndex = 3;162
this.setTextBackgroundWorkerBtn.Text = "Safe BW Call";163
this.setTextBackgroundWorkerBtn.Click += new System.EventHandler(this.setTextBackgroundWorkerBtn_Click);164
// 165
// backgroundWorker1166
// 167
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);168
// 169
// Form1170
// 171
this.ClientSize = new System.Drawing.Size(268, 96);172
this.Controls.Add(this.setTextBackgroundWorkerBtn);173
this.Controls.Add(this.setTextSafeBtn);174
this.Controls.Add(this.setTextUnsafeBtn);175
this.Controls.Add(this.textBox1);176
this.Name = "Form1";177
this.Text = "Form1";178
this.ResumeLayout(false);179
this.PerformLayout();180

181
}182

183
#endregion184

185

186
[STAThread]187
static void Main()188

{189
Application.EnableVisualStyles();190
Application.Run(new Form1());191
}192

193
}194
}195

196

浙公网安备 33010602011771号