ThreadPool的QueueUserWorkItem 方法 (WaitCallback)和QueueUserWorkItem 方法 (WaitCallback, Object)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   

註:要使用此方法都需要引入應用:using System.Threading;//引入應用

參數意義:將要執行的方法排入隊列以便執行,WaitCallback,即表示將要執行的方法;Object,包含方法所用資料的對象。如果將方法成功排入隊列,則為 true;否則為 false

一、下面是ThreadPool.QueueUserWorkItem 方法 (WaitCallback)的範例程式碼:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading;//引入應用 6  7 namespace QueueUserWorkItem_WaitCallback_ 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             // Queue the task.14             ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));15 16             Console.WriteLine("Main thread does some work, then sleeps.");17             // If you comment out the Sleep, the main thread exits before18             // the thread pool task runs.  The thread pool uses background19             // threads, which do not keep the application running.  (This20             // is a simple example of a race condition.)21             Thread.Sleep(1000);22 23             Console.WriteLine("Main thread exits.");24         }25 26         // This thread procedure performs the task.27         static void ThreadProc(Object stateInfo)28         {29             // No state object was passed to QueueUserWorkItem, so 30             // stateInfo is null.31             Console.WriteLine("Hello from the thread pool.");32         }33     }34 }

運行以上代碼後:

二、下面是ThreadPool.QueueUserWorkItem 方法 (WaitCallback, Object)的範例程式碼:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6  7 namespace QueueUserWorkItem__WaitCallback__Object_ 8 { 9     //將方法排入隊列以便執行,並指定包含該方法所用資料的對象。 此方法在有線程池線程變得可用時執行。10     /*11      * 12      * public static bool QueueUserWorkItem(WaitCallback callBack,Object state)13      * 14      * 15      * callBack  類型:System.Threading.WaitCallback16      * WaitCallback ,它表示要執行的方法。17      * 18      * 19      * state     類型:System.Object20      * 包含方法所用資料的對象。21      * 22      * 23      * 傳回值    類型:System.Boolean24      * 如果此方法成功排隊,則為 true;如果無法將該工作項目排隊,則引發 NotSupportedException。25      * 26      * 27      * 註:如果回調方法需要複雜資料,可以定義包含這些資料的類。28      * 29      */30     class Program31     {32         static void Main(string[] args)33         {34             // Create an object containing the information needed for the task.35             TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42);36 37             // Queue the task and data.38             ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti);39 40             Console.WriteLine("Main thread does some work, then sleeps.");41 42             // If you comment out the Sleep, the main thread exits before43             // the ThreadPool task has a chance to run.  ThreadPool uses 44             // background threads, which do not keep the application 45             // running.  (This is a simple example of a race condition.)46             Thread.Sleep(1000);47 48             Console.WriteLine("Main thread exits.");49         }50 51         // The thread procedure performs the independent task, in this case52         // formatting and printing a very simple report.53         //54         static void ThreadProc(Object stateInfo)55         {56             TaskInfo ti = (TaskInfo)stateInfo;57             Console.WriteLine(ti.Boilerplate, ti.Value);58         }59     }60 61     // TaskInfo holds state information for a task that will be62     // executed by a ThreadPool thread.63     public class TaskInfo64     {65         // State information for the task.  These members66         // can be implemented as read-only properties, read/write67         // properties with validation, and so on, as required.68         public string Boilerplate;69         public int Value;70 71         // Public constructor provides an easy way to supply all72         // the information needed for the task.73         public TaskInfo(string text, int number)74         {75             Boilerplate = text;76             Value = number;77         }78     }79 }

運行以上代碼後:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.