Beispiel #1
0
        private void InitProcessMonitor(ProcessStartInfo startInfo)
        {
            //Reset the Abort signal and register it with the remoted harness
            abortSignal.Reset();

            // Remember the arguments so tests can get to them easily
            // NOTE: startInfo may be null if we are monitoring without starting a process
            // If there are already values under ApplicationMonitorArguments, leave them and do nothing.
            if ((startInfo != null) && !string.IsNullOrEmpty(startInfo.Arguments) && string.IsNullOrEmpty(DictionaryStore.Current["ApplicationMonitorArguments"]))
            {
                DictionaryStore.Current["ApplicationMonitorArguments"] = startInfo.Arguments;
            }

            //Create the ProcessMonitor
            processMonitor = new ProcessMonitor();
            processMonitor.ProcessExited             += new ProcessExitedHandler(OnProcessExited);
            processMonitor.ProcessFound              += new ProcessFoundHandler(OnProcessFound);
            processMonitor.VisibleWindowFound        += new VisibleWindowHandler(OnVisibleWindowFound);
            processMonitor.VisibleWindowTitleChanged += new VisibleWindowHandler(OnVisibleWindowTitleChanged);

            //Monitor the Processes that the DeploymentHelper says are important in deployment
            processesToMonitor = ApplicationDeploymentHelper.GetProcessesToMonitor(startInfo);
            foreach (ProcessMonitorInfo processInfo in processesToMonitor)
            {
                processMonitor.AddProcess(processInfo.Name);
            }

            //Add Processes to be monitored that are described in the handler rules
            foreach (UIHandlerRule rule in handlerRules)
            {
                if (rule.ProcessName != null)
                {
                    processMonitor.AddProcess(rule.ProcessName);
                }
            }
        }
        private static void MonitorProcess(string filename, VisibleWindowHandler visibleWindowHandler, ProcessExitedHandler processExitHandler)
        {
            //Get the active window since the window activation will be lost by lauching the display properties
            IntPtr           activehWnd = IntPtr.Zero;
            ManualResetEvent waitEvent  = new ManualResetEvent(false);

            ProcessMonitor processMonitor     = new ProcessMonitor();
            Process        themeChangeProcess = new Process();

            themeChangeProcess.StartInfo.FileName = filename;

            try
            {
                //Initialize the wait event
                waitEvent.Reset();
                activehWnd = User32.GetForegroundWindow();

                //This field is set when the correct process is started and checked when any process exits
                int processId = 0;
                processMonitor.VisibleWindowFound += new VisibleWindowHandler(delegate(IntPtr topLevelhWnd, IntPtr hWnd, Process process, string title)
                {
                    if (process.ProcessName.ToLowerInvariant().Equals(ThemeProcessName))
                    {
                        processId = process.Id;
                        User32.SetForegroundWindow(topLevelhWnd);  //In case the dialog was opened previously
                        Thread.Sleep(2000);

                        if (visibleWindowHandler != null)
                        {
                            visibleWindowHandler(topLevelhWnd, hWnd, process, title);
                        }
                    }
                });

                processMonitor.ProcessExited += new ProcessExitedHandler(delegate(Process process)
                {
                    if (process.Id == processId)
                    {
                        if (processExitHandler != null)
                        {
                            processExitHandler(process);
                        }

                        Thread.Sleep(1000);     //For good measure
                        waitEvent.Set();
                    }
                });

                themeChangeProcess.Start();

                //Start monitoring processes
                processMonitor.AddProcess(ThemeProcessName);
                processMonitor.Start();

                waitEvent.WaitOne(60000, false);
            }
            finally
            {
                if (processMonitor != null)
                {
                    processMonitor.Stop();
                }

                //Restore the active window
                if (activehWnd != IntPtr.Zero)
                {
                    User32.SetForegroundWindow(activehWnd);
                }
            }
        }