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();
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);
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;
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 =
"线程做加法"
;
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;
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);
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;
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;
while
(ReturnInt < 5)
{
sign++;
if
(
this
.InvokeRequired)
{
this
.Invoke(
new
delegateAddDisplay(AddDisplay), sign);
}
else
{
AddDisplay(sign);
}
ReturnInt++;
System.Threading.Thread.Sleep(2000);
}
return
true
;
}
delegate
void
delegateAddDisplay(
int
sign);
delegate
void
delegateEndDisplay(
string
str);
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);
if
(ReturnBool)
{
if
(
this
.InvokeRequired)
{
this
.Invoke(
new
delegateEndDisplay(EndDisplay),
"事情已经做完"
);
}
else
{
EndDisplay(
"事情已经做完"
);
}
}
}
delegate
bool
Btn_ProcessMethod(
int
sign);
Btn_ProcessMethod ProcessMethod;
private
void
button2_Click(
object
sender, EventArgs e)
{
MessageBox.Show(
"你执行你的加法运算,我弹我的框,哈哈"
);
}
}
}