using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 异步委托和线程调用
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
#region 默认代码
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar progressBar1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(147, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(116, 28);
this.button1.TabIndex = 0;
this.button1.Text = "使用线程=>";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(147, 44);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(116, 28);
this.button2.TabIndex = 0;
this.button2.Text = "使用异步委托=>";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(0, 80);
this.progressBar1.Maximum = 1000;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(412, 12);
this.progressBar1.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(410, 94);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "Form1";
this.Text = "Form1";
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
#endregion
//线程
private void button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(
new System.Threading.ThreadStart(Fun2));
this.processEvent += new myProcessEventHandle(Form1_processEvent);
th.Start();
button2.Enabled = false;
}
//异步委托
private void button2_Click(object sender, System.EventArgs e)
{
System.Threading.WaitCallback wc = new System.Threading.WaitCallback(Fun);
object obj = this.button1;//这里没用。到时候你可以用来传递参数
this.processEvent += new myProcessEventHandle(Form1_processEvent);
System.Threading.ThreadPool.QueueUserWorkItem(wc,obj);
button1.Enabled = false;
}
public delegate void myProcessEventHandle(int percent);//定义事件
public event myProcessEventHandle processEvent = null;//事件声明
//线程无法带参数启动,和这个函数通讯直接用成员变量就可以了
private void Fun2()
{
int i = 0;
String str = this.Text;
while (i<1000)
{
i++;
//this.Text = i.ToString();
if (this.processEvent != null)
{
this.processEvent(i);
}
System.Threading.Thread.Sleep(8);
}
TelMe();
this.Text = str;
}
//异步委托可以也必须要一个参数,当然不想用就传一个null就可以了
private void Fun(object obj)
{
int i = 0;
String str = this.Text;
while (i<1000)
{
i++;
//this.Text = i.ToString();
if (this.processEvent != null)
{
this.processEvent(i);
}
System.Threading.Thread.Sleep(8);
}
TelMe();
this.Text = str;
}
//不是回调,但是同一个类里写的,就可以直接调用了。
private void TelMe()
{
this.processEvent -= new myProcessEventHandle(Form1_processEvent);
button1.Enabled = true;
button2.Enabled = true;
MessageBox.Show("操作完成!");
}
//事件方式通知主线程进度
private void Form1_processEvent(int percent)
{
this.progressBar1.Value = percent;
this.Text = ((double)percent/10).ToString() + "%";
}
private void Form1_Closed(object sender, System.EventArgs e)
{
System.Environment.Exit(0);//用于退出没有运行结束的线程
}
}
}
//以上代码在vs2003可以运行。以前写的例子。
评论