[Multithreading]关于ThreadPool.QueueUserWorkItem
2013-07-04 14:15
393 查看
将方法排入队列以便执行。此方法在有线程池线程变得可用时执行。——MSDN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication26 { class Program { static void Main(string[] args) { ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine("委托方式."); Console.WriteLine(string.Format("{0}-ThreadPool.QueueUserWorkItem Example By delegate", DateTime.Now)); PrintLine(); }); Console.WriteLine("普通方式"); WaitCallback _wCallBack = new WaitCallback(ProcessCallBack); ThreadPool.QueueUserWorkItem(_wCallBack, "ThreadPool.QueueUserWorkItem Example1 by Normal"); ThreadPool.QueueUserWorkItem(_wCallBack, "ThreadPool.QueueUserWorkItem Example2 by Normal"); ThreadPool.QueueUserWorkItem(_wCallBack, "ThreadPool.QueueUserWorkItem Example3 by Normal"); ThreadPool.QueueUserWorkItem(_wCallBack, "ThreadPool.QueueUserWorkItem Example4 by Normal"); PrintLine(); ThreadPool.QueueUserWorkItem(callBack => { Console.WriteLine("Lambda方式."); Console.WriteLine(string.Format("{0}-ThreadPool.QueueUserWorkItem Example by Lambda", DateTime.Now)); PrintLine(); }); Console.ReadLine(); } private static void ProcessCallBack(object state) { Console.WriteLine(string.Format("{0}-{1} start.", DateTime.Now, (string)state)); Thread.Sleep(500); Console.WriteLine(string.Format("{0}-{1} end.", DateTime.Now, (string)state)); } static void PrintLine() { Console.WriteLine("-----------------------------------------------------------------------------"); } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- 一个关于WaitCallback和ThreadPool.QueueUserWorkItem的小例子
- ThreadPool.QueueUserWorkItem的用法
- [原创] 难道调用ThreadPool.QueueUserWorkItem()的时候,真是必须调用Thread.Sleep(N)吗?
- ThreadPool.QueueUserWorkItem的性能问题
- ThreadPool.QueueUserWorkItem 方法 (WaitCallback)
- ThreadPool.QueueUserWorkItem 方法 (WaitCallback)
- 在 .NET Web 开发中,使用 ThreadPool.QueueUserWorkItem 异步地处理耗时操作
- 使用ThreadPool.QueueUserWorkItem记录网站错误日志
- WPF使用ThreadPool.QueueUserWorkItem线程池防界面假死
- ThreadPool.QueueUserWorkItem 用法
- 难道调用ThreadPool.QueueUserWorkItem()的时候,真是必须调用Thread.Sleep(N)吗?
- ThreadPool.QueueUserWorkItem的性能问题
- ThreadPool.QueueUserWorkItem引发的血案,线程池异步非正确姿势导致程序闪退的问题
- 多线程实现Thread.Start()与ThreadPool.QueueUserWorkItem两种方式对比
- ThreadPool.QueueUserWorkItem的性能问题
- ThreadPool.QueueUserWorkItem的性能问题
- ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta), new SomeState(0));
- ThreadPool.QueueUserWorkItem的性能问题
- ThreadPool.QueueUserWorkItem的性能问题
- [转]ThreadPool.QueueUserWorkItem的性能问题