using System; using System.Threading; namespace ConsoleApplication7 { class Program { static void Main(string[] args) { //ThreadPool.SetMinThreads(2, 0); int workerThreads, completionPortThreads; ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads); Console.WriteLine(workerThreads + ", " + completionPortThreads + "\n"); for (int i = 0; i <= 2; i++) ThreadPool.QueueUserWorkItem(new WaitCallback(WorkA), i + 1); Console.ReadKey(); } static void WorkA(Object obj) { for (int i = 0; i < 3; i++) { Console.WriteLine("執行緒{0}:", obj); Thread.Sleep(100); } } } }
ThreadPool.QueueUserWorkItem:可以將工作加入佇列裡面,等執行緒去處理
ThreadPool.SetMinThreads:可以設定最少的執行緒數量
設定『最少執行緒數量前』
設定『最少執行緒數量後』
參考資料:
http://msdn.microsoft.com/zh-tw/library/system.threading.threadpool(v=vs.110).aspx
http://msdn.microsoft.com/zh-tw/library/kbf0f1ct(v=vs.110).aspx
http://msdn.microsoft.com/zh-tw/library/system.threading.threadpool.setminthreads(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/vstudio/h4732ks0.aspx
http://www.dotblogs.com.tw/atowngit/archive/2009/12/20/12557.aspx
http://www.cnblogs.com/luminji/archive/2011/05/13/2044801.html
http://columns.chicken-house.net/post/ThreadPool-e5afa6e4bd9c-2-e7a88be5bc8fe7a2bc-(C).aspx
http://hillstone.blog.hexun.com.tw/53765753_d.html