异步回调能够不影响主线程继续操作 ,单独去完成一部分任务
public delegate void EventHandler(string s);
public event EventHandler a;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 声明事件触发
a += new EventHandler(d);
MessageBox.Show("即将引发事件!");
IAsyncResult result = a.BeginInvoke("1", new AsyncCallback(AddComplete), "");
// IAsyncResult result = a.BeginInvoke("1", null, "");
MessageBox.Show("触发完毕");
//a.EndInvoke(result);
MessageBox.Show("触发完毕1");
}
//实现
public void d(string s)
{
System.Threading.Thread.Sleep(3000);
MessageBox.Show("触发成功正在执行:"+s);
}
static void AddComplete(IAsyncResult result)
{
EventHandler handler = (EventHandler)((AsyncResult)result).AsyncDelegate;
handler.EndInvoke(result);
MessageBox.Show(result.IsCompleted.ToString());
}