Пример #1
0
            internal static int ExecuteCommandBlocking(
                string command, string parameters, string workingDir, string pathExtension,
                IDebuggerAttacher debuggerAttacher, Action <string> reportOutputLine, Action <int> reportProcessId)
            {
                using (var pipeStream = new ProcessOutputPipeStream())
                {
                    var processInfo = CreateProcess(command, parameters, workingDir, pathExtension, pipeStream._writingEnd);
                    reportProcessId(processInfo.dwProcessId);
                    using (var process = new SafeWaitHandle(processInfo.hProcess, true))
                        using (var thread = new SafeWaitHandle(processInfo.hThread, true))
                        {
                            pipeStream.ConnectedToChildProcess();

                            debuggerAttacher?.AttachDebugger(processInfo.dwProcessId);

                            ResumeThread(thread);

                            using (var reader = new StreamReader(pipeStream, Encoding.Default))
                                while (!reader.EndOfStream)
                                {
                                    reportOutputLine(reader.ReadLine());
                                }

                            WaitForSingleObject(process, INFINITE);

                            int exitCode;
                            if (!GetExitCodeProcess(process, out exitCode))
                            {
                                throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not get exit code of process");
                            }

                            return(exitCode);
                        }
                }
            }
Пример #2
0
            internal static int ExecuteCommandBlocking(
                string command, string parameters, string workingDir, string pathExtension,
                IDebuggerAttacher debuggerAttacher, DebuggerEngine debuggerEngine,
                ILogger logger, bool printTestOutput, Action <string> reportOutputLine, Action <int> reportProcessId)
            {
                ProcessOutputPipeStream pipeStream = null;

                try
                {
                    pipeStream = new ProcessOutputPipeStream();

                    var processInfo = CreateProcess(command, parameters, workingDir, pathExtension, pipeStream._writingEnd);
                    reportProcessId(processInfo.dwProcessId);
                    using (var process = new SafeWaitHandle(processInfo.hProcess, true))
                        using (var thread = new SafeWaitHandle(processInfo.hThread, true))
                        {
                            pipeStream.ConnectedToChildProcess();

                            logger.DebugInfo($"Attaching debugger to '{command}' via {debuggerEngine} engine");
                            if (!debuggerAttacher.AttachDebugger(processInfo.dwProcessId, debuggerEngine))
                            {
                                logger.LogError($"Could not attach debugger to process {processInfo.dwProcessId}");
                            }

                            if (printTestOutput)
                            {
                                DotNetProcessExecutor.LogStartOfOutput(logger, command, parameters);
                            }

                            ResumeThread(thread);

                            using (var reader = new StreamReader(pipeStream, Encoding.Default))
                            {
                                pipeStream = null;

                                while (!reader.EndOfStream)
                                {
                                    string line = reader.ReadLine();
                                    reportOutputLine(line);
                                    if (printTestOutput)
                                    {
                                        logger.LogInfo(line);
                                    }
                                }
                            }

                            WaitForSingleObject(process, INFINITE);

                            if (printTestOutput)
                            {
                                DotNetProcessExecutor.LogEndOfOutput(logger);
                            }

                            int exitCode;
                            if (!GetExitCodeProcess(process, out exitCode))
                            {
                                throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not get exit code of process");
                            }

                            return(exitCode);
                        }
                }
                finally
                {
                    pipeStream?.Dispose();
                }
            }