Example #1
0
        public bool WaitForExit(int milliseconds)
        {
            SafeProcessHandle safeProcessHandle = null;
            ProcessWaitHandle processWaitHandle = null;
            bool flag;

            try
            {
                safeProcessHandle = GetProcessHandle(1048576, false);
                if (safeProcessHandle.IsInvalid)
                {
                    flag = true;
                }
                else
                {
                    processWaitHandle = new ProcessWaitHandle(safeProcessHandle);
                    if (processWaitHandle.WaitOne(milliseconds, false))
                    {
                        flag     = true;
                        signaled = true;
                    }
                    else
                    {
                        flag     = false;
                        signaled = false;
                    }
                }
            }
            finally
            {
                if (processWaitHandle != null)
                {
                    processWaitHandle.Close();
                }
                if (output != null && milliseconds == -1)
                {
                    output.WaitUtilEOF();
                }
                if (error != null && milliseconds == -1)
                {
                    error.WaitUtilEOF();
                }
                ReleaseProcessHandle(safeProcessHandle);
            }
            if (flag && watchForExit)
            {
                RaiseOnExited();
            }
            return(flag);
        }
Example #2
0
        private SafeProcessHandle GetProcessHandle(int access, bool throwIfExited)
        {
            if (haveProcessHandle)
            {
                if (throwIfExited)
                {
                    ProcessWaitHandle processWaitHandle = null;
                    try
                    {
                        processWaitHandle = new ProcessWaitHandle(m_processHandle);
                        if (processWaitHandle.WaitOne(0, false))
                        {
                            if (haveProcessId)
                            {
                                throw new InvalidOperationException("ProcessHasExited");
                            }
                            throw new InvalidOperationException("ProcessHasExitedNoId");
                        }
                    }
                    finally
                    {
                        if (processWaitHandle != null)
                        {
                            processWaitHandle.Close();
                        }
                    }
                }
                return(m_processHandle);
            }
            EnsureState((Process2.State) 3);
            SafeProcessHandle safeProcessHandle = SafeProcessHandle.InvalidHandle;

            safeProcessHandle = ProcessManager.OpenProcess(processId, access, throwIfExited);
            if (throwIfExited && (access & 1024) != 0 && NativeMethods.GetExitCodeProcess(safeProcessHandle, out exitCode) && exitCode != 259)
            {
                throw new InvalidOperationException();
            }
            return(safeProcessHandle);
        }