delegate int Count(int a);
Count del =new Count(你的函数);
IAsyncReslut ar =del.BeginInvoke(10000,null,null);
.............
在程序结束最后
if(ar.IsCompleted)
{
int count=del.EndInvoke(ar);
}
else
{
ar.AsyncWaitHandle.WaitOne();
int count=del.EndInvoke(ar);}
或者可以用CALLBACK函数的
Count del =new Count(你的函数);
IAsyncReslut ar =del.BeginInvoke(10000,null,null);
.............
在程序结束最后
if(ar.IsCompleted)
{
int count=del.EndInvoke(ar);
}
else
{
ar.AsyncWaitHandle.WaitOne();
int count=del.EndInvoke(ar);}
或者可以用CALLBACK函数的
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;namespace WindowsApplication1
{
//定义代理
public delegate void OccuredHandler(string msg);
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//定义事件
public event OccuredHandler Occured;
//定义线程
Thread myThread;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
//注册事件
this.Occured +=new OccuredHandler(Form1_Occured);
myThread = new Thread(new ThreadStart(RunThread));
} /// <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.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(184, 32);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "运行";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 112);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(192, 23);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(184, 72);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//线程方法
public void RunThread()
{
for(int i=0;i<1000000;i++)
{
this.label1.Text = i.ToString();
}
//激发事件
Occured("运行完成!");
}
//线程结束通知函数
private void Form1_Occured(string msg)
{
MessageBox.Show(msg);
} private void button1_Click(object sender, System.EventArgs e)
{
myThread.Start();
this.button1.Enabled = false;
} private void button2_Click(object sender, System.EventArgs e)
{
//if(myThread.ThreadState!=ThreadState.Stopped||myThread.ThreadState!=ThreadState.Aborted)
myThread.Abort();
Application.Exit();
}
}
}