예제 #1
0
        private static Dictionary <int, string> GetProcessTitleMap(IEnumerable <Process> processes)
        {
            var res = new Dictionary <int, string>();

            foreach (var p in processes)
            {
                var title = EnumWindows.GetTitle(p.MainWindowHandle);
                res[p.Id] = title;
            }
            return(res);
        }
예제 #2
0
        public bool WaitUntilTitleStartsWith(string startsWith, int timeoutMs = 10000)
        {
            const int waitStep = 1000;
            var       end      = DateTime.UtcNow.AddMilliseconds(timeoutMs);

            while (true)
            {
                var title = EnumWindows.GetTitle(Process.MainWindowHandle);
                var good  = title.StartsWith(startsWith);
                var tdiff = end - DateTime.UtcNow;
                if (good || tdiff <= TimeSpan.Zero)
                {
                    return(good);
                }
                Thread.Sleep(Math.Min(waitStep, (int)tdiff.TotalMilliseconds));
            }
        }
예제 #3
0
        /// <summary>
        ///  Switch to the main window of the process.
        ///  If 'Process' is null a new process will be created.
        ///  If unable to get the window from the process will attempt to find the window
        ///  with <paramref name="titleMatch"/>.
        /// </summary>
        /// <param name="titleMatch">Match the title</param>
        /// <param name="titleWaitTimeOutMs">The time out for waiting the window</param>
        /// <returns>If successful</returns>
        private bool SwitchTo(Func <string, bool> titleMatch = null,
                              int titleWaitTimeOutMs         = 10000)
        {
            const int waitStep = 1000;

            var prevProcesses = GetAllExistingProcess();
            var prevTitleMap  = GetProcessTitleMap(prevProcesses);

            bool processWasNull = Process == null;

            if (processWasNull)
            {
                var psi = new ProcessStartInfo
                {
                    FileName  = ProcessPath,
                    Arguments = Arguments
                };
                Process = Process.Start(psi);
                Thread.Sleep(MaximizeDelay);
            }

            if (Process.HasExited)
            {
                var diffProcesses = GetDiff(prevTitleMap);
                if (diffProcesses.Length == 1 && titleMatch == null)
                {
                    Process = diffProcesses[0];
                    SwitchToThisWindow();
                    return(true);
                }
                else if (titleMatch != null)
                {
                    var lastReg = DateTime.UtcNow;
                    var end     = lastReg.AddMilliseconds(titleWaitTimeOutMs);
                    while (true)
                    {
                        var tdiff   = end - DateTime.UtcNow;
                        var elapsed = DateTime.UtcNow - lastReg;
                        if (elapsed.TotalMilliseconds > 3000)
                        {
                            diffProcesses = GetDiff(prevTitleMap);
                            lastReg       = DateTime.UtcNow;
                        }
                        foreach (var process in diffProcesses)
                        {
                            var title = EnumWindows.GetTitle(process.MainWindowHandle);
                            var found = titleMatch(title);
                            if (found)
                            {
                                Process = process;
                                SwitchToThisWindow();
                                return(true);
                            }
                            else if (tdiff <= TimeSpan.Zero)
                            {
                                return(false);
                            }
                        }
                        Thread.Sleep(Math.Min(waitStep, (int)tdiff.TotalMilliseconds));
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (titleMatch != null)
                {
                    var processes = GetAllExistingProcess();
                    var lastReg   = DateTime.UtcNow;
                    var end       = lastReg.AddMilliseconds(titleWaitTimeOutMs);
                    while (true)
                    {
                        var tdiff   = end - DateTime.UtcNow;
                        var elapsed = DateTime.UtcNow - lastReg;
                        if (elapsed.TotalMilliseconds > 3000)
                        {
                            processes = GetAllExistingProcess();
                            lastReg   = DateTime.UtcNow;
                        }
                        foreach (var process in processes)
                        {
                            var title = EnumWindows.GetTitle(process.MainWindowHandle);
                            if (titleMatch(title))
                            {
                                Process = process;
                                SwitchToThisWindow();
                                return(true);
                            }
                            else if (tdiff <= TimeSpan.Zero)
                            {
                                return(false);
                            }
                        }
                    }
                }
                else
                {
                    SwitchToThisWindow();
                    return(true);
                }
            }
        }