static void Main(string[] args) { int threadId = 0; RunOnThreadPool poolDelegate = Test; var t = new Thread(() => Test(out threadId)); t.Start(); t.Join(); Console.WriteLine("Thread id: {0}", threadId); IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call"); r.AsyncWaitHandle.WaitOne(); string result = poolDelegate.EndInvoke(out threadId, r); Console.WriteLine("线程池工作线程的: {0}", threadId); Console.WriteLine(result); Thread.Sleep(TimeSpan.FromSeconds(2)); Console.Read(); } private delegate string RunOnThreadPool(out int threadId); private static void Callback(IAsyncResult ar) { Console.WriteLine("开始回调..."); Console.WriteLine("State passed to a callbak: {0}", ar.AsyncState); Console.WriteLine("是一个线程池线程: {0}", Thread.CurrentThread.IsThreadPoolThread); Console.WriteLine("线程池线程ID是: {0}", Thread.CurrentThread.ManagedThreadId); } private static string Test(out int threadId) { Console.WriteLine("开始..."); Console.WriteLine("是一个线程池线程: {0}", Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep(TimeSpan.FromSeconds(2)); threadId = Thread.CurrentThread.ManagedThreadId; return string.Format("线程池工作线程的ID是: {0}", threadId); }
C#多线程之线程池篇一APM(Asynchronous Programming Model)
原文:https://www.cnblogs.com/gougou1981/p/12381723.html