using System.Collections;
using System.Diagnostics;
using System;
using System.ComponentModel;
namespace ThreadPoolDemo
{
class ThreadPoolDemo1
{
public ThreadPoolDemo1()
{
}
public void Work()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(CountProcess));
ThreadPool.QueueUserWorkItem(new WaitCallback(GetEnvironmentVariables));
}
/// <summary>
/// 统计当前正在运行的系统进程信息
/// </summary>
/// <param name="state"></param>
private void CountProcess(object state)
{
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
try
{
Console.WriteLine("Id:{0},ProcessName:{1},StartTime:{2}", p.Id, p.ProcessName, p.StartTime);
}
catch (Win32Exception e)
{
Console.WriteLine("ProcessName:{0}", p.ProcessName);
}
finally
{
}
}
Console.WriteLine("获取进程信息完毕。");
}
/// <summary>
/// 获取当前机器系统变量设置
/// </summary>
/// <param name="state"></param>
public void GetEnvironmentVariables(object state)
{
IDictionary list=System.Environment.GetEnvironmentVariables();
foreach (DictionaryEntry item in list)
{
Console.WriteLine("key={0},value={1}", item.Key, item.Value);
}
Console.WriteLine("获取系统变量信息完毕。");
}
static void Main(string[] args)
{
ThreadPoolDemo1 tpd1 = new ThreadPoolDemo1();
tpd1.Work();
Thread.Sleep(5000);
Console.WriteLine("OK");
Console.ReadLine();
}
}
}
上面这段代码在本机的运行情况如下: key=Path,value=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\BINN;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;C:\MySQL Server 5.1\bin;C:\php-5.2.9-Win32 key=TEMP,value=C:\DOCUME~1\ZHOUFO~1\LOCALS~1\Temp key=SESSIONNAME,value=Console key=PATHEXT,value=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH key=Rav,value=C:\Documents and Settings\All Users\Application Data\Rising\Rav key=PROCESSOR_ARCHITECTURE,value=x86 key=SystemDrive,value=C: key=APPDATA,value=C:\Documents and Settings\zhoufoxcn\Application Data key=windir,value=C:\WINDOWS key=USERPROFILE,value=C:\Documents and Settings\zhoufoxcn key=TMP,value=C:\DOCUME~1\ZHOUFO~1\LOCALS~1\Temp key=USERDOMAIN,value=ZHOU key=ProgramFiles,value=C:\Program Files key=FP_NO_HOST_CHECK,value=NO key=HOMEPATH,value=\Documents and Settings\zhoufoxcn key=COMPUTERNAME,value=ZHOU key=USERNAME,value=zhoufoxcn key=NUMBER_OF_PROCESSORS,value=2 key=PROCESSOR_IDENTIFIER,value=x86 Family 6 Model 15 Stepping 13, GenuineIntel key=SystemRoot,value=C:\WINDOWS key=ComSpec,value=C:\WINDOWS\system32\cmd.exe key=LOGONSERVER,value=\\ZHOU key=CommonProgramFiles,value=C:\Program Files\Common Files key=PROMPT,value=$P$G key=PROCESSOR_LEVEL,value=6 key=PROCESSOR_REVISION,value=0f0d key=VS80COMNTOOLS,value=C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\ key=lib,value=C:\Program Files\SQLXML 4.0\bin\ key=ALLUSERSPROFILE,value=C:\Documents and Settings\All Users key=VS90COMNTOOLS,value=c:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\ key=OS,value=Windows_NT key=HOMEDRIVE,value=C: 获取系统变量信息完毕。 Id:1864,ProcessName:mysqld,StartTime:2010-1-11 8:23:25 Id:3732,ProcessName:HTime,StartTime:2010-1-11 8:32:16 Id:1328,ProcessName:spoolsv,StartTime:2010-1-11 8:23:23 Id:3908,ProcessName:ctfmon,StartTime:2010-1-11 8:32:19 Id:3640,ProcessName:cmd,StartTime:2010-1-11 15:43:04 Id:1488,ProcessName:notepad,StartTime:2010-1-11 15:30:48 Id:3668,ProcessName:conime,StartTime:2010-1-11 15:27:22 Id:964,ProcessName:svchost,StartTime:2010-1-11 8:23:22 Id:1408,ProcessName:svchost,StartTime:2010-1-11 8:23:23 Id:1140,ProcessName:svchost,StartTime:2010-1-11 8:23:22 Id:1940,ProcessName:sqlbrowser,StartTime:2010-1-11 8:23:25 Id:1672,ProcessName:MsDtsSrvr,StartTime:2010-1-11 8:23:24 Id:3540,ProcessName:explorer,StartTime:2010-1-11 8:32:14 Id:3268,ProcessName:wps,StartTime:2010-1-11 14:08:00 Id:1568,ProcessName:inetinfo,StartTime:2010-1-11 8:23:24 Id:588,ProcessName:csrss,StartTime:2010-1-11 8:23:17 Id:3704,ProcessName:360tray,StartTime:2010-1-11 8:32:16 Id:1028,ProcessName:svchost,StartTime:2010-1-11 8:23:22 Id:2184,ProcessName:mqtgsvc,StartTime:2010-1-11 8:23:28 Id:2628,ProcessName:Reflector,StartTime:2010-1-11 15:04:35 Id:3872,ProcessName:devenv,StartTime:2010-1-11 15:26:51 Id:204,ProcessName:ThreadPoolDemo,StartTime:2010-1-11 15:43:26 Id:664,ProcessName:winlogon,StartTime:2010-1-11 8:23:20 Id:840,ProcessName:dexplore,StartTime:2010-1-11 14:50:21 Id:3900,ProcessName:rundll32,StartTime:2010-1-11 8:32:18 Id:1636,ProcessName:mdm,StartTime:2010-1-11 8:23:24 Id:1012,ProcessName:RavMonD,StartTime:2010-1-11 8:23:22 Id:1100,ProcessName:svchost,StartTime:2010-1-11 8:23:22 Id:476,ProcessName:smss,StartTime:2010-1-11 8:23:14 Id:920,ProcessName:svchost,StartTime:2010-1-11 8:23:21 Id:716,ProcessName:services,StartTime:2010-1-11 8:23:21 Id:3052,ProcessName:ThreadPoolDemo.vshost,StartTime:2010-1-11 15:42:27 Id:1448,ProcessName:msdtc,StartTime:2010-1-11 8:23:23 Id:180,ProcessName:mqsvc,StartTime:2010-1-11 8:23:25 Id:2512,ProcessName:iexplore,StartTime:2010-1-11 14:52:29 Id:1888,ProcessName:nvsvc32,StartTime:2010-1-11 8:23:25 Id:728,ProcessName:lsass,StartTime:2010-1-11 8:23:21 Id:2240,ProcessName:alg,StartTime:2010-1-11 8:23:28 Id:3808,ProcessName:jusched,StartTime:2010-1-11 8:32:17 Id:3128,ProcessName:RsTray,StartTime:2010-1-11 8:32:17 Id:1992,ProcessName:svchost,StartTime:2010-1-11 8:23:25 Id:2944,ProcessName:Foxit Reader,StartTime:2010-1-11 14:08:48 Id:4,ProcessName:System,StartTime:1601-1-1 8:00:00 Id:1604,ProcessName:jqs,StartTime:2010-1-11 8:23:24 ProcessName:Idle 获取进程信息完毕。 OK 在上面的代码中我们使用了线程池,并让它执行了两个任务,一个是列出系统当前所有环境变量的值,一个是列出系统当前运行的进程名和它们的启动时间。 当然,优点和缺点总是同时存在的,使用ThreadPool也有一些缺点,使用线程池有如下缺点: 1、一旦加入到线程池中就没有办法让它停止,除非任务执行完毕自动停止; 2、一个进程共享一个线程池; 3、要执行的任务不能有返回值(当然,线程中要执行的方法也是不能有返回值,如果确实需要返回值必须采用其它技巧来解决); 4、在线程池中所有任务的优先级都是一样的,无法设置任务的优先级; 5、不太适合需要长期执行的任务(比如在Windows服务中执行),也不适合大的任务; 6、不能为线程设置稳定的关联标识,比如为线程池中执行某个特定任务的线程指定名称或者其它属性。 如果我们要面临的情况正好是线程池的缺点,那么我们只好继续使用线程而不是线程池。不过在某些情况下使用线程池确实可以带来很多方便的,比如在WEB服务器中,可以使用线程池来处理来自客户端的请求,可以以比较高的性能运行。
本文出自 “周公(周金桥)的专栏” 博客,请务必保留此出处http://zhoufoxcn.blog.51cto.com/792419/263696