首页 > 代码库 > ThreadPool的QueueUserWorkItem 方法 (WaitCallback)和QueueUserWorkItem 方法 (WaitCallback, Object)

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

注:要使用此方法都需要引入应用: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 =http://www.mamicode.com/ number;77         }78     }79 }

运行以上代码后: