Ejemplo n.º 1
0
        private void ExecuteImpl_NoOutput(Process process)
        {
            try
            {
                process.Start();
            }
            catch (Win32Exception win32e)
            {
                OnProcessStartWin32Exception?.Invoke(win32e);
                return;
            }
            catch (Exception)
            {
                return;
            }

            if (process.WaitForExit(ExecutableTimeout))
            {
                ProcessExit?.Invoke();
            }
            else
            {
                try
                {
                    process.Kill();
                    OnProcessTimeout?.Invoke();
                }
                catch (Win32Exception) { }
                catch (NotSupportedException) { }
                catch (InvalidOperationException) { }
            }
        }
Ejemplo n.º 2
0
        private void ExecuteImpl_ReadOutput(Process process)
        {
            StringBuilder output = new StringBuilder();
            StringBuilder error  = new StringBuilder();

            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                {
                    process.OutputDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            outputWaitHandle.Set();
                        }
                        else
                        {
                            output.AppendLine(e.Data);
                        }
                    };

                    process.ErrorDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            errorWaitHandle.Set();
                        }
                        else
                        {
                            error.AppendLine(e.Data);
                        }
                    };

                    try
                    {
                        process.Start();
                    }
                    catch (Win32Exception win32e)
                    {
                        OnProcessStartWin32Exception?.Invoke(win32e);
                        return;
                    }
                    catch (Exception)
                    {
                        return;
                    }

                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    if (process.WaitForExit(ExecutableTimeout) &&
                        outputWaitHandle.WaitOne() &&
                        errorWaitHandle.WaitOne())
                    {
                        ProcessOutput?.Invoke(output, error);
                        ProcessExit?.Invoke();
                    }
                    else
                    {
                        try
                        {
                            process.Kill();
                            OnProcessTimeout?.Invoke();
                        }
                        catch (Win32Exception) { }
                        catch (NotSupportedException) { }
                        catch (InvalidOperationException) { }
                    }
                }
        }