注:要使用此方法都需要引入应用: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 }

运行以上代码后:

最新文章

  1. 删除Windows 服务
  2. Spring MVC+Maven+Freemarker+Mybatis开发环境搭建
  3. 基于 ArcGIS Silverlight API开发的WebGIS应用程序的部署
  4. IE比Chrome强的一个地方
  5. fixSidebar简介与修正log
  6. 日版 Galaxy Note sc05d 涮机
  7. 快速解读GC日志(转)
  8. Python操作redis、memcache和ORM框架_Day13
  9. Linux SSH使用公钥私钥实现免登陆
  10. CSS3笔记之第一天
  11. 九度OJ题目1003:A+B
  12. ubunru18.04下面安装docker
  13. springboot+freemarker毕业设计项目错误合集
  14. python数据类型一:字符串
  15. 制作Win10 U盘版移动便携系统
  16. python之路-格式化输出、编码
  17. Python学习:列表、元组、字典、集合
  18. 自动化测试基础篇--Selenium简单的163邮箱登录实例
  19. Servlet 监听器Listener详解
  20. Java并发编程原理与实战三十二:ForkJoin框架详解

热门文章

  1. python做一个数独小游戏
  2. phpstudy初级总结
  3. 06使用NanoPiM1Plus在Android4.4.2下接U盘
  4. Linux终端常用快捷操作
  5. Django - 自定义simple_tag
  6. Python之IO编程
  7. bzoj2693 jzptab 莫比乌斯反演|题解
  8. node-sass安装失败的解决方案
  9. mysql服务无法启动(1067错误)时数据备份的经验
  10. 关于vuex自己理解的三幅图