C#异步委托调用实例分析


本文实例讲述了C#异步委托调用实现方法。分享给大家供大家参考。具体如下:

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));
}

希望本文所述对大家的C#程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3