Process类--MSDN转载

Process 组件提供对正在计算机上运行的进程的访问。用最简短的话来说,进程就是当前运行的应用程序。线程是操作系统向其分配处理器时间的基本单位。线程可执行进程的任何一部分代码,包括当前由另一线程执行的部分。

对于启动、停止、控制和监视应用程序等任务,Process 组件是很有用的工具。使用 Process 组件,可以获取正在运行的进程的列表,或者可以启动新的进程。Process 组件用于访问系统进程。初始化 Process 组件后,可使用该组件来获取有关当前运行的进程的信息。此类信息包括线程集、加载的模块(.dll 和 .exe 文件)和性能信息(如进程当前使用的内存量)。

如果在系统中用引号声明了一个路径变量,则在启动该位置中的任何进程时,必须完全限定该路径。否则,系统将找不到该路径。例如,如果 c:\mypath 不在您的路径中,而您使用引号添加它 (path = %path%;"c:\mypath"),则在启动 c:\mypath 中的任何进程时,您必须完全限定它们。

进程组件同时获取有关一组属性的信息。Process 组件获取有关任一组的一个成员的信息后,它将缓存该组中其他属性的值,并且在您调用 Refresh 方法之前,不获取有关该组中其他成员的新信息。因此,不保证属性值比对 Refresh 方法的最后一次调用更新。组细分与操作系统有关。

系统进程在系统上由其进程标识符唯一标识。与许多 Windows 资源一样,进程也由其句柄标识,而句柄在计算机上可能不唯一。句柄是表示资源标识符的一般术语。即使进程已退出,操作系统仍保持进程句柄,该句柄通过 Process 组件的 Handle 属性访问。因此,可以获取进程的管理信息,如 ExitCode(通常,或者为零表示成功,或者为非零错误代码)和 ExitTime句柄是非常有价值的资源,所以句柄泄漏比内存泄漏危害更大。

例:

下面的示例使用 Process 类的实例启动进程。

using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{    

  class MyProcess    
  {        
    public static void Main()        
    {            
      Process myProcess = new Process();            
      try
             { 
               myProcess.StartInfo.UseShellExecute = false;
                // You can start any process, HelloWorld is a do-nothing example.
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                myProcess.StartInfo.CreateNoWindow = true; 
               myProcess.Start();
                // This code assumes the process you are starting will terminate itself. 
                // Given that is is started without a window so you cannot terminate it
                 // on the desktop, it must terminate itself or you can do it programmatically 
               // from this application using the Kill method. 
           } 
           catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        } 
   }
}

 

下面的示例使用 Process 类本身和静态 Start 方法启动进程。

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
	/// <summary>
	/// Shell for the sample.
	/// </summary>
	class MyProcess
	{
	   
		/// <summary>
		/// Opens the Internet Explorer application.
		/// </summary>
		void OpenApplication(string myFavoritesPath)
		{
			// Start Internet Explorer. Defaults to the home page.
			Process.Start("IExplore.exe");
				    
		    // Display the contents of the favorites folder in the browser.
		    Process.Start(myFavoritesPath);

		}
		
		/// <summary>
		/// Opens urls and .html documents using Internet Explorer.
		/// </summary>
		void OpenWithArguments()
		{
			// url's are not considered documents. They can only be opened
			// by passing them as arguments.
			Process.Start("IExplore.exe", "www.northwindtraders.com");
			
			// Start a Web page using a browser associated with .html and .asp files.
			Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
			Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
		}
		
		/// <summary>
		/// Uses the ProcessStartInfo class to start new processes, both in a minimized 
		/// mode.
		/// </summary>
		void OpenWithStartInfo()
		{
			
			ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
			startInfo.WindowStyle = ProcessWindowStyle.Minimized;
			
			Process.Start(startInfo);
			
			startInfo.Arguments = "www.northwindtraders.com";
			
			Process.Start(startInfo);
			
		}

		static void Main()
		{
            		// Get the path that stores favorite links.
            		string myFavoritesPath = 
                	Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

            		MyProcess myProcess = new MyProcess();

			myProcess.OpenApplication(myFavoritesPath);
			myProcess.OpenWithArguments();
			myProcess.OpenWithStartInfo();

       		}	
	}
}

posted @ 2010-09-16 10:52  小狮子在悬崖边做题  阅读(886)  评论(0编辑  收藏  举报