为什么hasExited抛出'System.ComponentModel.Win32Exception'?
问题描述:
我可以使用ShellExecuteEx成功运行一个新进程,但是如果我访问HasExited属性,则会引发Win32Exception。什么是这个异常,我如何检查过程是否还活着?为什么hasExited抛出'System.ComponentModel.Win32Exception'?
ShellExecuteEx(ref info);
clientProcessId = NativeMethods.GetProcessId(info.hProcess);
Process clientProcess = Process.GetProcessById((int)clientProcessId);
if (clientProcess.HasExited) //<---- throws Win32Exception, but the process is alive! I can see its window
{
//run new one
}
感谢
答
有关信息,并设置fMask到SEE_MASK_NOCLOSEPROCESS
,使hProcess是有效的?另外,你为什么要对ShellExecuteEx进行P/Invoke'ing,为什么不使用Process.Start w/ProcessStartInfo,它会为你处理ShellExecuteEx?
编辑:由于您正在做runas,您只能在句柄上获得SYNCHRONIZE访问权限,而不是PROCESS_QUERY_INFORMATION
访问权限,因此GetExitCodeProcess失败,导致hasEnded抛出Win32异常。
作为一种解决方法,您可以使用超时为零的P/Invoke WaitForSingleObject来查看进程是否已退出。
答
因为对象犯规存在了,因此你的检查被摧毁了一个对象可能引发错误
与图片打交道时类似的问题出现在VB
function openimg(imgpath)
img = get image from path
return img
end function
由于某种原因不会正确返回图像
替代线路必须
回报img.clone()正确返回图像
但是这就是我的例子
答
我今天遇到了这个问题。还有另一种解决方案。 WaitForExit函数在另一个用户环境中运行良好。 因此,如果您执行runas以提升进程权限,则可以使用此函数查询进程状态。
你能发布一段代码吗? – 2009-04-21 21:43:40
我已经更新了这个问题。 – mimic 2009-04-21 22:10:16