运行control.exe作为进程不WaitForExit()

时间:2012-09-04 20:55:44

标签: waitforexit

我正在运行control.exe作为进程。 (Windows 7操作系统,.NET 3.5,C#)。它不会按预期在WaitForExit()停止。即使control.exe窗口仍然打开,它立即“退出”进程。我已经尝试了process.Exited事件,也是在应用程序退出之前触发的事件。 这是我的代码:

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


            process.Start();

            process.WaitForExit();

1 个答案:

答案 0 :(得分: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; }
 }

您的程序将一直停留,直到控件关闭​​。如果控制永远不会再次关闭,则应插入转义序列;)

相关问题