2012-09-04 1 views
0

control.exe를 프로세스로 실행 중입니다. (윈도우 7 OS, .NET 3.5, C#). WaitForExit()에서 예상대로 멈추지 않습니다. control.exe 창이 여전히 열려 있어도 프로세스를 즉시 "종료"합니다. 나는 또한 프로세스를 시도했다. 종료 된 이벤트 또한 응용 프로그램이 종료되기 전에 트리거된다. 창이 표시하거나 작업을 같이 실행중인 경우control.exe를 프로세스로 실행하면 WaitForExit()이 발생하지 않습니다.

  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; } 
} 

프로그램이 종료 될 때까지 프로그램이 멈 춥니 다.

관련 문제