标签:net oid chap void 使用 执行 一个 back sleep
创建线程是昂贵的操作,为每一个短暂的异步操作创建线程会产生显著的开销。
1 using System; 2 using System.Threading; 3 4 namespace Chapter3.Recipe1 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 int threadId = 0; 11 12 RunOnThreadPool poolDelegate = Test; 13 14 var t = new Thread(() => Test(out threadId)); 15 t.Start(); 16 t.Join(); 17 18 Console.WriteLine("Thread id: {0}", threadId); 19 20 //调用BeginInvoke()运行委托,回调函数在异步操作完成后被调用 21 //BeginInvoke()调用后会立即返回,线程池中的工作线程在执行异步操作时,仍允许继续其他工作。 22 IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call"); 23 r.AsyncWaitHandle.WaitOne(); //等待异步操作完成 24 //事实上EndInvoke()会等待异步操作的完成,所以r.AsyncWaitHandle.WaitOne();并不是必要的 25 string result = poolDelegate.EndInvoke(out threadId, r); 26 //异步操作完成后,回调函数会被放置到线程池中,确切的说是一个工作线程中。 27 Console.WriteLine("Thread pool worker thread id: {0}", threadId); 28 Console.WriteLine(result); 29 30 Thread.Sleep(TimeSpan.FromSeconds(2)); 31 } 32 33 private delegate string RunOnThreadPool(out int threadId); 34 35 private static void Callback(IAsyncResult ar)//回调函数 36 { 37 Console.WriteLine("Starting a callback..."); 38 Console.WriteLine("State passed to a callbak: {0}", ar.AsyncState); 39 Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread); 40 Console.WriteLine("Thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId); 41 } 42 43 44 private static string Test(out int threadId) 45 { 46 Console.WriteLine("Starting..."); 47 Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread); 48 Thread.Sleep(TimeSpan.FromSeconds(2)); 49 threadId = Thread.CurrentThread.ManagedThreadId; 50 return string.Format("Thread pool worker thread id was: {0}", threadId); 51 } 52 } 53 }
使用BeginOperationName/EndOperation方法和.NET中的IAsyncResult对象等方式被称为异步编程模型,这样的方法对被称为异步方法。
标签:net oid chap void 使用 执行 一个 back sleep
原文地址:https://www.cnblogs.com/larry-xia/p/9263000.html