private void analyzerOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { if (_thread == null) { // We got here because the environment is shutting down, the current object was disposed and the thread was aborted. return; } String output = outLine.Data; if (!String.IsNullOrEmpty(output)) { addProblemsToToolwindow(parseOutput(output)); try { CPPCheckPluginPackage.addTextToOutputWindow(output + "\n"); } catch (Exception) { } } }
private void startAnalyzerProcess(string analyzerExePath, string arguments) { System.Diagnostics.Process process = null; try { Debug.Assert(!String.IsNullOrEmpty(analyzerExePath)); Debug.Assert(!String.IsNullOrEmpty(arguments)); process = new System.Diagnostics.Process(); process.StartInfo.FileName = analyzerExePath; process.StartInfo.WorkingDirectory = Path.GetDirectoryName(analyzerExePath); process.StartInfo.Arguments = arguments; process.StartInfo.CreateNoWindow = true; // Set UseShellExecute to false for output redirection. process.StartInfo.UseShellExecute = false; // Redirect the standard output of the command. process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; // Set our event handler to asynchronously read the sort output. process.OutputDataReceived += new DataReceivedEventHandler(this.analyzerOutputHandler); process.ErrorDataReceived += new DataReceivedEventHandler(this.analyzerOutputHandler); CPPCheckPluginPackage.addTextToOutputWindow("Starting analyzer with arguments: " + arguments + "\n"); var timer = Stopwatch.StartNew(); // Start the process. process.Start(); try { process.PriorityClass = ProcessPriorityClass.Idle; } catch (System.InvalidOperationException) { } onProgressUpdated(0); // Start the asynchronous read of the sort output stream. process.BeginOutputReadLine(); process.BeginErrorReadLine(); // Wait for analysis completion while (!process.WaitForExit(500)) { if (_terminateThread) { // finally block will run anyway and do the cleanup return; } } timer.Stop(); analysisFinished(arguments); if (process.ExitCode != 0) { CPPCheckPluginPackage.addTextToOutputWindow(analyzerExePath + " has exited with code " + process.ExitCode.ToString() + "\n"); } else { double timeElapsed = Math.Round(timer.Elapsed.TotalSeconds, 3); CPPCheckPluginPackage.addTextToOutputWindow("Analysis completed in " + timeElapsed.ToString() + " seconds\n"); } process.Close(); process = null; } catch (Exception ex) { DebugTracer.Trace(ex); } finally { onProgressUpdated(100); if (process != null) { try { process.Kill(); } catch (Exception ex) { DebugTracer.Trace(ex); } process.Dispose(); } } }