c# - 有没有一种好方法可以将结果从外部进程流式传输到 Visual Studio 输出 Pane 中?

标签 c# stream external-process vs-extensibility vspackage

我在 VsPackage 中设置了一个自定义输出 Pane ,类似于以下内容:

    ///--------------------------------------------------------------------------------
    /// <summary>This property gets the custom output pane.</summary>
    ///--------------------------------------------------------------------------------
    private Guid _customPaneGuid = Guid.Empty;
    private IVsOutputWindowPane _customPane = null;
    private IVsOutputWindowPane customPane
    {
        get
        {
            if (_customPane == null)
            {
                IVsOutputWindow outputWindow = GetService(typeof(SVsOutputWindow)) as IVsOutputWindow;
                if (outputWindow != null)
                {
                    // look for existing solution updater pane
                    if (_customPaneGuid == Guid.Empty || ErrorHandler.Failed(outputWindow.GetPane(ref _customPaneGuid, out _customPane)) || _customPane == null)
                    {
                        // create a new solution updater pane
                        outputWindow.CreatePane(ref _customPaneGuid, "My Output", 1, 1);
                        if (ErrorHandler.Failed(outputWindow.GetPane(ref _customPaneGuid, out _customPane)) || _customPane == null)
                        {
                            // pane could not be created and retrieved, throw exception
                            throw new Exception("Custom pane could not be created and/or retrieved");
                        }
                    }
                }
            }
            if (_customPane != null)
            {
                _customPane.Activate();
            }
            return _customPane;
        }
    }

并且使用类似于以下的方法将消息发送到此 Pane :

    ///--------------------------------------------------------------------------------
    /// <summary>This method displays a message in the output area.</summary>
    /// 
    /// <param name="outputTitle">The title for the message.</param>
    /// <param name="outputMessage">The message to show.</param>
    /// <param name="appendMessage">Flag indicating whether message should be appended to existing message.</param>
    ///--------------------------------------------------------------------------------
    public void ShowOutput(string outputTitle, string outputMessage, bool appendMessage, bool isException)
    {
        if (appendMessage == false)
        {
            // clear output pane
            CustomPane.Clear();
        }

        if (outputTitle != string.Empty)
        {
            // put output title to output pane
            CustomPane.OutputString("\r\n" + outputTitle);
        }

        // put output message to output pane
        CustomPane.OutputString("\r\n" + outputMessage);

        if (isException == true)
        {
            // show message box
            MessageBox.Show(outputTitle + "\r\n" + outputMessage, outputTitle);
        }
    }

我有一个外部进程,它将当前解决方案的诊断结果发送到控制台。它的设置类似于以下内容:

///-------------------------------------------------------------------------------- 
/// <summary>This method handles clicking on the Run Diagnostics submenu.</summary> 
///  
/// <param term='inputCommandBarControl'>The control that is source of the click.</param> 
/// <param term='handled'>Handled flag.</param> 
/// <param term='cancelDefault'>Cancel default flag.</param> 
///-------------------------------------------------------------------------------- 
protected void RunDiagnostics_Click(object inputCommandBarControl, ref bool handled, ref bool cancelDefault) 
{ 
    try 
    { 
        // set up and execute diagnostics thread 
        RunDiagnosticsDelegate RunDiagnosticsDelegate = RunDiagnostics; 
        RunDiagnosticsDelegate.BeginInvoke(RunDiagnosticsCompleted, RunDiagnosticsDelegate); 
    } 
    catch (Exception ex) 
    { 
        // put exception message in output pane 
        CustomPane.OutputString(ex.Message); 
    } 
} 

protected delegate void RunDiagnosticsDelegate(); 

///-------------------------------------------------------------------------------- 
/// <summary>This method launches the diagnostics to review the solution.</summary> 
///-------------------------------------------------------------------------------- 
protected void RunDiagnostics() 
{ 
    try 
    { 
        // set up diagnostics process 
        string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName); 
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(@"MyDiagnostics.exe", solutionDir); 
        procStartInfo.RedirectStandardOutput = true; 
        procStartInfo.UseShellExecute = false; 
        procStartInfo.CreateNoWindow = true; 
        System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
        proc.StartInfo = procStartInfo; 

        // execute the diagnostics 
        proc.Start(); 

        // put diagnostics output to output pane 
        CustomPane.OutputString(proc.StandardOutput.ReadToEnd()); 
        CustomPane.OutputString("Diagnostics run complete."); 
    } 
    catch (Exception ex) 
    { 
        // put exception message in output pane 
        CustomPane.OutputString(ex.Message); 
    } 
} 

///-------------------------------------------------------------------------------- 
/// <summary>This method handles completing the run diagnostics thread.</summary> 
///  
/// <param name="ar">IAsyncResult.</param> 
///-------------------------------------------------------------------------------- 
protected void RunDiagnosticsCompleted(IAsyncResult ar) 
{ 
    try 
    { 
        if (ar == null) throw new ArgumentNullException("ar"); 

        RunDiagnosticsDelegate RunDiagnosticsDelegate = ar.AsyncState as RunDiagnosticsDelegate; 
        Trace.Assert(RunDiagnosticsDelegate != null, "Invalid object type"); 

        RunDiagnosticsDelegate.EndInvoke(ar); 
    } 
    catch (Exception ex) 
    { 
        // put exception message in output pane 
        CustomPane.OutputString(ex.Message); 
    } 
} 

当我从 VSPackage 启动此 external process 时,我想将这些结果(间接)流式传输到自定义输出 Pane ,显示消息作为诊断工具报告他们。有什么好的方法吗?

最佳答案

显然,您必须使用 Process.BeginOutputReadLine() 方法。可以在此处找到示例:System.Diagnostics.Process.BeginOutputReadLine .

关于c# - 有没有一种好方法可以将结果从外部进程流式传输到 Visual Studio 输出 Pane 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8345636/

相关文章:

c# - 如何指示 Entity Framework 不要更新模型的属性

c# - 如果在 Entity Framework 中使用延迟加载,如何序列化实体?

c# - RSA解密异常: The length of the data to decrypt is not valid for the size of this key

javascript - 使用 XMLHttpRequest 的内存高效消息 block 处理

perl - 当 Perl 中的命令执行失败时,如何读取 stderr?

performance - Julia 中的并行实现比串行慢

c# - 将 Newtonsoft JSON 的 JToken 转换为 JArray 的最佳方法是什么?

c# - Stream EndRead何时阻塞回调

java - 接收整数序列的服务器

r - 使用系统中的多个命令从 R 调用外部程序