C# - Making a Process.Start wait until the process has start-up

前端 未结 12 2111
迷失自我
迷失自我 2020-11-27 17:12

I need to make sure that a process is running before moving on with a method.

The statement is:

Process.Start(\"popup.exe\");

Can y

12条回答
  •  -上瘾入骨i
    2020-11-27 17:49

    public static class WinApi
    {
    
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
        public static class Windows
        {
            public const int NORMAL = 1;
            public const int HIDE = 0;
            public const int RESTORE = 9;
            public const int SHOW = 5;
            public const int MAXIMIXED = 3;
        }
    
    }
    

    App

    String process_name = "notepad"
    Process process;
    process = Process.Start( process_name );
    
    while (!WinApi.ShowWindow(process.MainWindowHandle, WinApi.Windows.NORMAL))
    {
        Thread.Sleep(100);
        process.Refresh();
    }
    
    // Done!
    // Continue your code here ...
    

提交回复
热议问题