2012-09-04 20 views
0

我正在運行control.exe作爲一個進程。 (Windows 7操作系統,.NET 3.5,C#)。它不會像預期的那樣在WaitForExit()處停止。即使control.exe窗口仍處於打開狀態,它立即「退出」該進程。我已經嘗試了該過程.Exited事件也是在應用程序退出之前觸發的。 這裏是我的代碼:作爲進程運行control.exe不WaitForForit()

  Process process = new Process(); 
      process.StartInfo.FileName = @"c:\windows\system32\control.exe"; 
      process.StartInfo.Arguments = @"userpasswords"; 


      process.Start(); 

      process.WaitForExit(); 
+2

如果你看看任務管理器,你會發現'control.exe'沒有運行,所以'WaitForExit'是正確的。這個過程已經退出。 –

回答

0

從手機發送(請糾正我的格式,會佔用幾個小時通過電話)

嘗試檢查,如果窗口是可見的或任務運行,如:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Reflection; 
using System.Runtime.InteropServices; 
using System.Diagnostics; 

namespace yournamespace 
{ 
    class FormCheck 
    { 
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] 
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle); 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, StringBuilder lParam); 

    const int WM_GETTEXT = 0x000D; 
    const int WM_GETTEXTLENGTH = 0x000E; 

    public static bool IsRunning(string window, bool exactfit) 
    { return Check(window,exactfit); } 

    public static bool IsRunning(string window) 
    { return Check(window); } 

    public static bool Check(string Processname) 
    { 
     Process[] pname = Process.GetProcessesByName(Processname); 
     if (pname.Length <= 1) { return false; } else { return true; } 
    } 

    public static bool Check(string WindowTitle, bool exactfit) 
    { 
     bool response = false; 
     string[] strWindowsTitles = EnumerateOpenedWindows.GetDesktopWindowsTitles(); 
     string strResponse = ""; 
     foreach (string strTitle in strWindowsTitles) 
     { 
      if (strTitle.Contains(WindowTitle)) 
      { 
       if (exactfit) 
       { 
        if (strTitle == WindowTitle) 
        { 
         strResponse = strTitle; 
         break; 
        } 
       } 
       else 
       { 
        if (strTitle.Contains(WindowTitle)) 
        { 
         strResponse = strTitle; 
         break; 
        } 
       } 
      } 
     } 

     string pid = string.Empty; 
     if (strResponse != "") 
     { 
      response = true; 
     } 
     else { response = false; } 

     return response; 
     } 

    } 

    public class EnumerateOpenedWindows 
    { 
    const int MAXTITLE = 255; 
    private static List<string> lstTitles; 
    private delegate bool EnumDelegate(IntPtr hWnd, int lParam); 
    [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", 
    ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool EnumDesktopWindows(IntPtr hDesktop, 
    EnumDelegate lpEnumCallbackFunction, IntPtr lParam); 
    [DllImport("user32.dll", EntryPoint = "GetWindowText", 
    ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern int _GetWindowText(IntPtr hWnd, 
    StringBuilder lpWindowText, int nMaxCount); 
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool IsWindowVisible(IntPtr hWnd); 
    private static bool EnumWindowsProc(IntPtr hWnd, int lParam) 
    { 
     string strTitle = GetWindowText(hWnd); 
     if (strTitle != "" & IsWindowVisible(hWnd)) // 
     { 
      lstTitles.Add(strTitle); 
     } 
     return true; 
    } 
    /// <summary> 
    /// Return the window title of handle 
    /// </summary> 
    /// <param name="hWnd"></param> 
    /// <returns></returns> 
    public static string GetWindowText(IntPtr hWnd) 
    { 
     StringBuilder strbTitle = new StringBuilder(MAXTITLE); 
     int nLength = _GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1); 
     strbTitle.Length = nLength; 
     return strbTitle.ToString(); 
    } 
    /// <summary> 
    /// Return titles of all visible windows on desktop 
    /// </summary> 
    /// <returns>List of titles in type of string</returns> 
    public static string[] GetDesktopWindowsTitles() 
    { 
     lstTitles = new List<string>(); 
     EnumDelegate delEnumfunc = new EnumDelegate(EnumWindowsProc); 
     bool bSuccessful = EnumDesktopWindows(IntPtr.Zero, delEnumfunc, IntPtr.Zero); //for current desktop 
     if (bSuccessful) 
     { 
      return lstTitles.ToArray(); 
     } 
     else 
     { 
      // Get the last Win32 error code 
      int nErrorCode = Marshal.GetLastWin32Error(); 
      string strErrMsg = String.Format("EnumDesktopWindows failed  with code {0}.", nErrorCode); 
       throw new Exception(strErrMsg); 
      } 
     } 
    } 
} 

通過使用的的方法的一個 「isrunning」 等:

int waiter = 0; 
    while (isRunning("Control") 
{ 
//add 1 to an value and set to zero after x counts 
waiter++; 
if (waiter == 1000) { waiter=0; } 
} 

您的程序將卡住,直到控制關閉。如果控制不會再次關閉,應該插入轉義序列;)

相關問題