- using System.Threading;
- 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();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/3003201512022.html
来源: http://www.codesnippet.cn/detail/3003201512022.html