publicclass TestProcess : IDisposable { private Process process; public TestProcess(Process processToWrap) { process = processToWrap; } public Process Process { get { return process; } } IDisposable Members#region IDisposable Members publicvoid Dispose() { if (process !=null) { try { // give a bit of time if the process is exiting right now process.WaitForExit(5000); if (!process.HasExited) { // time to kill it process.Kill(); } } catch (Exception) { // this is "best effort" to kill the process don't allow any exceptions to get thrown. } process =null; } } #endregion }
[2] 启动被使用的程序,
使用using 后会自动调用Dispose()方法进行销毁
using (TestProcess licAdminProcess =new TestProcess(Process.Start(protectorPath))) { //这里我对已经Lanuch起来的程序开始一些其它的操作.如有异常,它不会阻止被使用程序的关闭! }
- Make people around you successful is the biggest contribution to ourselves. -