您的位置:首页 > 产品设计 > UI/UE

[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; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: