- static void Main(string[] args)
- {
- //委托异步
- Action<string> showMessage = ShowMessage;
- IAsyncResult result = showMessage.BeginInvoke("测试异步委托",null, null);
- //那在异步线程未完成工作以前主线程将处于阻塞状态,等到异步线程结束,主线程才能继续工作
- showMessage.EndInvoke(result);
- Console.ReadLine();
- }
- static void ShowMessage(string x)
- {
- string current = string.Format("当前线程id为{0}",Thread.CurrentThread.ManagedThreadId);
- Thread.Sleep(1000);
- Console.WriteLine(string.Format("{0},输入为{1}", current, x));
- }
- //该片段来自于http://www.codesnippet.cn/detail/010820134913.html
来源: http://www.codesnippet.cn/detail/010820134913.html