Beispiel #1
0
        public void WaitForProcess(TimeSpan waitTimeout)
        {
            SelectQuery checkProcess = new SelectQuery("Select * from Win32_Process Where ProcessId = " + this.processId);

            using (ManagementObjectSearcher processSearcher = new ManagementObjectSearcher(this.ManagementScope, checkProcess))
            {
                using (ManagementObjectCollection moc = processSearcher.Get())
                {
                    if (moc.Count == 0)
                    {
                        Log.Error("ERROR AS WARNING: Process " + this.command + " terminated before it could be tracked on " + this.machine);
                    }
                }
            }

            // Try to start necessary services to make ManagementEventWatcher work
            //try
            //{
            //    this.RunCommandInline("net start RpcLocator");
            //    this.RunCommandInline("net start RemoteRegistry");
            //    this.RunCommandInline("net start RasAuto");
            //    this.RunCommandInline("net start RasMan");
            //}
            //catch (Exception ex)
            //{
            //    ExceptionHelper.CentralProcess(ex);
            //}
            //finally
            //{
            //}

            try
            {
                #region Better waiting methodology
                WqlEventQuery wqlEventQuery = new WqlEventQuery("Win32_ProccessStopTrace");
                using (ManagementEventWatcher watcher = new ManagementEventWatcher(this.ManagementScope, wqlEventQuery))
                {
                    watcher.EventArrived += new EventArrivedEventHandler(this.ProcessStopEventArrived);
                    watcher.Start();
                    if (!this.ManualResetEventForWatchingEventArrived.WaitOne(waitTimeout, false))
                    {
                        watcher.Stop();
                        this.EventArrived = false;
                    }
                    else
                    {
                        watcher.Stop();
                    }
                }

                if (!this.EventArrived)
                {
                    SelectQuery sq = new SelectQuery("Select * from Win32_Process Where ProcessId = " + this.processId);
                    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(this.ManagementScope, sq))
                    {
                        foreach (ManagementObject obj in searcher.Get())
                        {
                            obj.InvokeMethod("Terminate", null);
                            obj.Dispose();
                            throw new Exception("Process " + this.command + " timed out and was killed on " + this.machine);
                        }
                    }
                }
                else
                {
                    if (this.ExitCode != 0)
                    {
                        throw new Exception("Process " + this.command + " exited with exit code " + this.ExitCode + " on " + this.machine + " run as " + this.userName);
                    }
                    else
                    {
                        Log.Info("Process {0} exited with Exit Code 0", this.processId);
                    }
                }
                #endregion Better waiting methodology
            }
            catch (Exception ex)
            {
                ExceptionHelper.CentralProcess(ex);

                #region Safer waiting methodology
                try
                {
                    Log.Info("Waiting for process {0}, command: {1} to finish on remote machine {2}", this.processId, this.command, this.machine);
                    ThreadHelper.PollCondition pollCondition = new ThreadHelper.PollCondition(delegate()
                    {
                        if (this.ProcessExists())
                        {
                            return(false);
                        }
                        else
                        {
                            return(true);
                        }
                    });

                    bool timedOut = !ThreadHelper.PollWait(pollCondition, TimeSpan.FromSeconds(30), waitTimeout);
                    if (timedOut)
                    {
                        Log.Info("Timed out after {2} second(s) in waiting process id {0} on remote machine {1}, exit waiting.", processId, machine, waitTimeout.TotalSeconds);
                    }
                    else
                    {
                        Log.Info("Process id: {0} exited on remote machine {1}.".FormatWith(processId, machine));
                    }
                }
                catch (Exception exception)
                {
                    // Make sure this exception has been logged.
                    ExceptionHelper.CentralProcess(exception);
                    throw ex;
                }
                #endregion Safer waiting methodology
            }
        }
        public void WaitForProcess(TimeSpan waitTimeout)
        {
            SelectQuery checkProcess = new SelectQuery("Select * from Win32_Process Where ProcessId = " + this.processId);
            using (ManagementObjectSearcher processSearcher = new ManagementObjectSearcher(this.ManagementScope, checkProcess))
            {
                using (ManagementObjectCollection moc = processSearcher.Get())
                {
                    if (moc.Count == 0)
                    {
                        Log.Error("ERROR AS WARNING: Process " + this.command + " terminated before it could be tracked on " + this.machine);
                    }
                }
            }

            // Try to start necessary services to make ManagementEventWatcher work
            try
            {
                this.RunCommandInline("net start RpcLocator");
                this.RunCommandInline("net start RemoteRegistry");
                this.RunCommandInline("net start RasAuto");
                this.RunCommandInline("net start RasMan");
            }
            catch (Exception ex)
            {
                ExceptionHelper.CentralProcess(ex);
            }
            finally
            {
            }

            try
            {
                #region Better waiting methodology
                WqlEventQuery wqlEventQuery = new WqlEventQuery("Win32_ProccessStopTrace");
                using (ManagementEventWatcher watcher = new ManagementEventWatcher(this.ManagementScope, wqlEventQuery))
                {
                    watcher.EventArrived += new EventArrivedEventHandler(this.ProcessStopEventArrived);
                    watcher.Start();
                    if (!this.ManualResetEventForWatchingEventArrived.WaitOne(waitTimeout, false))
                    {
                        watcher.Stop();
                        this.EventArrived = false;
                    }
                    else
                    {
                        watcher.Stop();
                    }
                }

                if (!this.EventArrived)
                {
                    SelectQuery sq = new SelectQuery("Select * from Win32_Process Where ProcessId = " + this.processId);
                    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(this.ManagementScope, sq))
                    {
                        foreach (ManagementObject obj in searcher.Get())
                        {
                            obj.InvokeMethod("Terminate", null);
                            obj.Dispose();
                            throw new Exception("Process " + this.command + " timed out and was killed on " + this.machine);
                        }
                    }
                }
                else
                {
                    if (this.ExitCode != 0)
                    {
                        throw new Exception("Process " + this.command + " exited with exit code " + this.ExitCode + " on " + this.machine + " run as " + this.userName);
                    }
                    else
                    {
                        Log.Info("Process {0} exited with Exit Code 0", this.processId);
                    }
                }
                #endregion Better waiting methodology
            }
            catch (Exception ex)
            {
                ExceptionHelper.CentralProcess(ex);

                #region Safer waiting methodology
                try
                {
                    Log.Info("Waiting for process {0}, command: {1} to finish on remote machine {2}", this.processId, this.command, this.machine);
                    ThreadHelper.PollCondition pollCondition = new ThreadHelper.PollCondition(delegate()
                    {
                        if (this.ProcessExists())
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    });

                    bool timedOut = !ThreadHelper.PollWait(pollCondition, TimeSpan.FromSeconds(30), waitTimeout);
                    if (timedOut)
                        Log.Info("Timed out after {2} second(s) in waiting process id {0} on remote machine {1}, exit waiting.", processId, machine, waitTimeout.TotalSeconds);
                    else
                        Log.Info("Process id: {0} exited on remote machine {1}.".FormatWith(processId, machine));
                }
                catch (Exception exception)
                {
                    // Make sure this exception has been logged.
                    ExceptionHelper.CentralProcess(exception);
                    throw ex;
                }
                #endregion Safer waiting methodology
            }
        }