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 Program
10     {
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 before
18             // the thread pool task runs.  The thread pool uses background
19             // threads, which do not keep the application running.  (This
20             // 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.WaitCallback
16      * WaitCallback ,它表示要執行的方法。
17      * 
18      * 
19      * state     類型:System.Object
20      * 包含方法所用數據的對象。
21      * 
22      * 
23      * 返回值    類型:System.Boolean
24      * 如果此方法成功排隊,則為 true;如果無法將該工作項排隊,則引發 NotSupportedException。
25      * 
26      * 
27      * 注:如果回調方法需要復雜數據,可以定義包含這些數據的類。
28      * 
29      */
30     class Program
31     {
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 before
43             // 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 case
52         // 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 be
62     // executed by a ThreadPool thread.
63     public class TaskInfo
64     {
65         // State information for the task.  These members
66         // can be implemented as read-only properties, read/write
67         // 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 all
72         // the information needed for the task.
73         public TaskInfo(string text, int number)
74         {
75             Boilerplate = text;
76             Value = number;
77         }
78     }
79 }

運行以上代碼后:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM