private void button2_Click(object sender, System.EventArgs e)
...{
AsyncCallback m_test=new AsyncCallback(TestSleep);
AsyncCallback m_wakeup=new AsyncCallback(WakeUp);
ar=m_test.BeginInvoke(null,m_wakeup,null);
System.Threading.ThreadStart m_ts=new System.Threading.ThreadStart(Monitor);
System.Threading.Thread m_thread=new System.Threading.Thread(m_ts);
m_thread.Start();
MyDelegatetest m_cxydele=new MyDelegatetest(ShowName);
m_cxydele.BeginInvoke(12,m_wakeup,null);
MessageBox.Show("I was clicked");
}
IAsyncResult ar;
public delegate void MyDelegatetest(int name);
private void TestSleep(IAsyncResult ar)
...{
Debug.WriteLine("调用开始了");
System.Threading.Thread.Sleep(5000);
MessageBox.Show("我醒了");
Debug.WriteLine("调用结束了");
}
private void WakeUp(IAsyncResult ar)
...{
MessageBox.Show("我醒了,要吃东西");
}
private void Monitor()
...{
while(!ar.IsCompleted)
...{
}
MessageBox.Show("对方已经执行完了,我们开始干活吧");
}
private void ShowName(int name)
...{
MessageBox.Show(name.ToString());
}