using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsApplication1
{
public
partial
class
Form1 : Form
{
#region 窗体设计器代码
/// <summary>
/// 必需的设计器变量。
/// </summary>
private
System.ComponentModel.IContainer components =
null
;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing && (components !=
null
))
{
components.Dispose();
}
base
.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private
void
InitializeComponent()
{
this
.button1 =
new
System.Windows.Forms.Button();
this
.textBox1 =
new
System.Windows.Forms.TextBox();
this
.label1 =
new
System.Windows.Forms.Label();
this
.textBox2 =
new
System.Windows.Forms.TextBox();
this
.button2 =
new
System.Windows.Forms.Button();
this
.SuspendLayout();
//
// button1
//
this
.button1.Location =
new
System.Drawing.Point(58, 119);
this
.button1.Name =
"button1"
;
this
.button1.Size =
new
System.Drawing.Size(75, 23);
this
.button1.TabIndex = 0;
this
.button1.Text =
"start"
;
this
.button1.UseVisualStyleBackColor =
true
;
this
.button1.Click +=
new
System.EventHandler(
this
.button1_Click);
//
// textBox1
//
this
.textBox1.Location =
new
System.Drawing.Point(128, 63);
this
.textBox1.Name =
"textBox1"
;
this
.textBox1.Size =
new
System.Drawing.Size(100, 20);
this
.textBox1.TabIndex = 1;
//
// label1
//
this
.label1.AutoSize =
true
;
this
.label1.Location =
new
System.Drawing.Point(55, 66);
this
.label1.Name =
"label1"
;
this
.label1.Size =
new
System.Drawing.Size(67, 13);
this
.label1.TabIndex = 2;
this
.label1.Text =
"线程做加法"
;
//
// textBox2
//
this
.textBox2.Location =
new
System.Drawing.Point(287, 63);
this
.textBox2.Name =
"textBox2"
;
this
.textBox2.Size =
new
System.Drawing.Size(100, 20);
this
.textBox2.TabIndex = 3;
//
// button2
//
this
.button2.Location =
new
System.Drawing.Point(157, 119);
this
.button2.Name =
"button2"
;
this
.button2.Size =
new
System.Drawing.Size(240, 23);
this
.button2.TabIndex = 4;
this
.button2.Text =
"执行start按钮期间,点击我弹Message"
;
this
.button2.UseVisualStyleBackColor =
true
;
this
.button2.Click +=
new
System.EventHandler(
this
.button2_Click);
//
// Form1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(437, 249);
this
.Controls.Add(
this
.button2);
this
.Controls.Add(
this
.textBox2);
this
.Controls.Add(
this
.label1);
this
.Controls.Add(
this
.textBox1);
this
.Controls.Add(
this
.button1);
this
.Name =
"Form1"
;
this
.Text =
"Form1"
;
this
.ResumeLayout(
false
);
this
.PerformLayout();
}
#endregion
private
System.Windows.Forms.Button button1;
private
System.Windows.Forms.TextBox textBox1;
private
System.Windows.Forms.Label label1;
private
System.Windows.Forms.TextBox textBox2;
private
System.Windows.Forms.Button button2;
#endregion
public
Form1()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
int
startInt =10;
//从10开始加起
ProcessMethod =
new
Btn_ProcessMethod(CallFun);
ProcessMethod.BeginInvoke(startInt,
new
AsyncCallback(CallBackFun),
null
);
}
/// <summary>
/// 委托调用的方法(主要是做一件事情;把textBox1中的数字加1加5次,返回事情已经做完的结果状态)
/// </summary>
/// <param name="sign"></param>
/// <returns></returns>
private
bool
CallFun(
int
sign)
{
int
ReturnInt = 0;
//返回加1的次数
while
(ReturnInt < 5)
//加5次
{
sign++;
if
(
this
.InvokeRequired)
//如果不是当前UI线程则返回true
{
this
.Invoke(
new
delegateAddDisplay(AddDisplay), sign);
}
else
{
AddDisplay(sign);
}
ReturnInt++;
System.Threading.Thread.Sleep(2000);
//两秒加一次
}
return
true
;
}
delegate
void
delegateAddDisplay(
int
sign);
//显示textBox1信息委托
delegate
void
delegateEndDisplay(
string
str);
//显示textBox2信息委托
private
void
AddDisplay(
int
sign)
{
this
.textBox1.Text = sign.ToString();
}
private
void
EndDisplay(
string
str)
{
this
.textBox2.Text = str;
}
/// <summary>
/// 异步回调方法
/// </summary>
/// <param name="IAresult"></param>
/// <returns></returns>
public
void
CallBackFun(IAsyncResult IAresult)
{
bool
ReturnBool = ProcessMethod.EndInvoke(IAresult);
//得到CallFun方法的返回值
if
(ReturnBool)
//CallFun方法返回True,说明CallFun方法已经执行完毕
{
if
(
this
.InvokeRequired)
//如果不是当前UI线程则返回true
{
//因为textBox2是UI线程创建的控件,所以要判断当前是不是创建此控件的UI线程
//(不然,就出现你所说的错误:“ 线程间操作无效:从不是创建控件 的线程访问它”)
this
.Invoke(
new
delegateEndDisplay(EndDisplay),
"事情已经做完"
);
}
else
{
EndDisplay(
"事情已经做完"
);
}
}
}
delegate
bool
Btn_ProcessMethod(
int
sign);
//UI以外线程代理
Btn_ProcessMethod ProcessMethod;
//UI以外线程代理申明
private
void
button2_Click(
object
sender, EventArgs e)
{
MessageBox.Show(
"你执行你的加法运算,我弹我的框,哈哈"
);
}
}
}