2009-04-21 91 views
0

我可以使用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 
} 

感謝

+0

你能發佈一段代碼嗎? – 2009-04-21 21:43:40

+0

我已經更新了這個問題。 – mimic 2009-04-21 22:10:16

回答

0

有關信息,並設置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來查看進程是否已退出。

-1

因爲對象犯規存在了,因此你的檢查被摧毀了一個對象可能引發錯誤

與圖片打交道時

類似的問題出現在VB

function openimg(imgpath) 
img = get image from path 
return img 
end function 

由於某種原因不會正確返回圖像

替代線路必須

回報img.clone()正確返回圖像

但是這就是我的例子

0

我今天遇到了這個問題。還有另一種解決方案。 WaitForExit函數在另一個用戶環境中運行良好。 因此,如果您執行runas以提升進程權限,則可以使用此函數查詢進程狀態。

相關問題