Почему процесс всегда не простаивает, когда я использую process.WaitForInputIdle?

#c#

#c#

Вопрос:

Я хочу использовать C #, чтобы вывести какое-либо приложение на передний план, и я использую WaitForInputIdle before SetForegroundWindow , и WaitForInputIdle всегда блокируется, если я устанавливаю параметр timeout, он возвращает false. Вот мой код:

 using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace AppToFront
{
    class Program
    {
        [DllImport("USER32.DLL")]
        static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);

        [DllImport("USER32.DLL")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        //Definition for Window Placement Structure
        [StructLayout(LayoutKind.Sequential)]
        private struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public int showCmd;
            public System.Drawing.Point ptMinPosition;
            public System.Drawing.Point ptMaxPosition;
            public System.Drawing.Rectangle rcNormalPosition;
        }

        //Definitions For Different Window Placement Constants
        const UInt32 SW_HIDE = 0;
        const UInt32 SW_SHOWNORMAL = 1;
        const UInt32 SW_NORMAL = 1;
        const UInt32 SW_SHOWMINIMIZED = 2;
        const UInt32 SW_SHOWMAXIMIZED = 3;
        const UInt32 SW_MAXIMIZE = 3;
        const UInt32 SW_SHOWNOACTIVATE = 4;
        const UInt32 SW_SHOW = 5;
        const UInt32 SW_MINIMIZE = 6;
        const UInt32 SW_SHOWMINNOACTIVE = 7;
        const UInt32 SW_SHOWNA = 8;
        const UInt32 SW_RESTORE = 9;

        public static void BringFont(string name)
        {
            //Checks if application is running
            try
            {
                Process process = Process.GetProcessesByName(name)[0];
                if (process != null)
                {
                    process.Refresh();
                    bool status = process.WaitForInputIdle(1000);
                    Console.WriteLine(status);
                    IntPtr s = process.MainWindowHandle;

                    WINDOWPLACEMENT param = new WINDOWPLACEMENT();

                    param.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));

                    GetWindowPlacement(s, ref param);

                    Console.WriteLine(param.showCmd);
                    if (param.showCmd == 1 || param.showCmd == 3)
                    {
                        SetForegroundWindow(s);
                    }
                    else
                    {
                        param.showCmd = 1;
                        SetWindowPlacement(s, ref param);
                    }
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("ERROR: Application is not running!nException: "   exc.Message);
                Console.ReadKey();
                return;
            }


        }

        static void Main(string[] args)
        {
            BringFont("chrome");
            Console.WriteLine("start");


        }

    }
}
  

Комментарии:

1. хорошо, это потому, что я получил ошибку process при использовании Process process = Process.GetProcessesByName(name)[0];