//获取一个已中断进程的退出代码,非零表示成功,零表示失败。
//参数hProcess,想获取退出代码的一个进程的句柄,参数lpExitCode,用于装载进程退出代码的一个长整数变量。
[DllImport("Kernel32.dll")]
static extern bool GetExitCodeProcess(System.IntPtr hProcess, ref uint lpExitCode);
#endregion
static public bool RunAndWait(string argm,bool IsShow)
{
STARTUPINFO sInfo = new STARTUPINFO();
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
if (!CreateProcess(null, new StringBuilder(argm), null, null, false, 0, null, null, ref sInfo, ref pInfo))
{
throw new Exception("调用失败");
}
uint i = 0;
WaitForSingleObject(pInfo.hProcess, int.MaxValue);
GetExitCodeProcess(pInfo.hProcess, ref i);
CloseHandle(pInfo.hProcess);
CloseHandle(pInfo.hThread);
return false;
}
private Process proc = null;
本文介绍了一个C#方法,用于启动进程并等待其结束,通过使用CreateProcess创建进程,并利用WaitForSingleObject和GetExitCodeProcess确保主程序等待子进程完成。此外,还展示了如何关闭进程句柄。
5005

被折叠的 条评论
为什么被折叠?



