2012-09-04 19 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; } 
} 

您的程序将卡住,直到控制关闭。如果控制不会再次关闭,应该插入转义序列;)

相关问题