C# 将文本流输出到VS2010输出窗口
我已经为VS2010创建了一个外接程序,它使用一个单独的类来进行一些处理。结果是文本显示在输出窗口的新窗格中 此处理类的构造函数引用由addins Connect类创建的新OutputWindowPane。文本是使用OutputString方法编写的 这可以正常工作,并且文本显示正确,但是,当执行返回到addins Connect类时,所有文本都会在一次更新中显示。我还注意到,当加载项运行时,IDE似乎冻结了。我是addin development的新手,所以我是否错过了一些明显的东西 是否有一种方法可以修改此过程,以便在每次调用OutputString时在输出窗口中更新文本?我希望有某种更新方法,但一直找不到 更新 我注意到这与使用单独的类无关。以下示例说明了我的问题:C# 将文本流输出到VS2010输出窗口,c#,visual-studio-2010,visual-studio,add-in,C#,Visual Studio 2010,Visual Studio,Add In,我已经为VS2010创建了一个外接程序,它使用一个单独的类来进行一些处理。结果是文本显示在输出窗口的新窗格中 此处理类的构造函数引用由addins Connect类创建的新OutputWindowPane。文本是使用OutputString方法编写的 这可以正常工作,并且文本显示正确,但是,当执行返回到addins Connect类时,所有文本都会在一次更新中显示。我还注意到,当加载项运行时,IDE似乎冻结了。我是addin development的新手,所以我是否错过了一些明显的东西 是否有一
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
namespace MyAddin4
{
public class Connect : IDTExtensibility2, IDTCommandTarget
{
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
string toolsMenuName = "Tools";
CommandBar menuBarCommandBar = ((CommandBars)_applicationObject.CommandBars)["MenuBar"];
CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
try
{
Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin4", "MyAddin4", "Executes the command for MyAddin4", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
if((command != null) && (toolsPopup != null))
{
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch(ArgumentException) {}
}
}
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) {}
public void OnAddInsUpdate(ref Array custom) {}
public void OnStartupComplete(ref Array custom) {}
public void OnBeginShutdown(ref Array custom) {}
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "MyAddin4.Connect.MyAddin4")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
return;
}
}
}
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin4.Connect.MyAddin4")
{
OutputWindow outputWindow = (OutputWindow) _applicationObject.Windows.Item(Constants.vsWindowKindOutput).Object;
OutputWindowPane outputPane = outputWindow.OutputWindowPanes.Add("Processor");
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
outputPane.Activate();
outputPane.OutputString(i + "\n");
}
handled = true;
return;
}
}
}
private DTE2 _applicationObject;
private AddIn _addInInstance;
}
}
使用系统;
使用可扩展性;
使用EnvDTE;
使用EnvDTE80;
使用Microsoft.VisualStudio.CommandBars;
名称空间MyAddin4
{
公共类连接:IDTExtensibility2、IDTCommandTarget
{
public void OnConnection(对象应用程序、ext_ConnectMode ConnectMode、对象外接程序、ref数组自定义)
{
_applicationObject=(DTE2)应用程序;
_附加值=(附加值)附加值;
if(connectMode==ext\u connectMode.ext\u cm\u ui设置)
{
object[]contextGUIDS=新对象[]{};
Commands2 commands=(Commands2)\u applicationObject.commands;
字符串toolsemuname=“Tools”;
CommandBar菜单栏CommandBar=((CommandBars)_applicationObject.CommandBars)[“菜单栏”];
CommandBarControl toolsControl=menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup=(CommandBarPopup)toolsControl;
尝试
{
Command Command=commands.AddNamedCommand2(_addin实例,“MyAddin4”,“MyAddin4”,“为MyAddin4执行命令”,true,59,ref contextGUIDS,(int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled,(int)vsCommandStyle.vsCommandStylePictAndText,vsCommandControlType.vsCommandControlTypeButton);
如果((命令!=null)&&(工具spopup!=null))
{
command.AddControl(toolsPopup.CommandBar,1);
}
}
捕获(参数异常){}
}
}
public void OnDisconnection(ext_DisconnectMode DisconnectMode,ref数组custom){}
公共void OnAddInsUpdate(ref数组自定义){}
public void OnStartupComplete(ref数组自定义){}
public void OnBeginShutdown(ref数组自定义){}
public void QueryStatus(字符串commandName、vsCommandStatusTextWanted NeedText、ref vsCommandStatus状态、ref对象commandText)
{
if(neededText==vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName==“MyAddin4.Connect.MyAddin4”)
{
状态=(vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
返回;
}
}
}
public void Exec(string commandName、vsCommandExecOption executeOption、ref object varIn、ref object varOut、ref bool handled)
{
已处理=错误;
if(executeOption==vsCommandExecOption.vscommandexecoptionodefault)
{
if(commandName==“MyAddin4.Connect.MyAddin4”)
{
OutputWindow OutputWindow=(OutputWindow)\u applicationObject.Windows.Item(Constants.vsWindowKindOutput.Object);
OutputWindowPane outputPane=outputWindow.OutputWindowPanes.Add(“处理器”);
对于(int i=0;i<10;i++)
{
系统.线程.线程.睡眠(500);
outputPane.Activate();
outputPane.OutputString(i+“\n”);
}
已处理=正确;
返回;
}
}
}
私有DTE2_应用对象;
私人外接程序;
}
}
解决方案
基于@ShellShock的答案,这是一个适合我的解决方案:
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
namespace MyAddin4
{
public class Connect : IDTExtensibility2, IDTCommandTarget
{
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;
string toolsMenuName = "Tools";
CommandBar menuBarCommandBar = ((CommandBars)_applicationObject.CommandBars)["MenuBar"];
CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
try
{
Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin4", "MyAddin4", "Executes the command for MyAddin4", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
if((command != null) && (toolsPopup != null))
{
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch(ArgumentException) {}
}
}
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) {}
public void OnAddInsUpdate(ref Array custom) {}
public void OnStartupComplete(ref Array custom) {}
public void OnBeginShutdown(ref Array custom) {}
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "MyAddin4.Connect.MyAddin4")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
return;
}
}
}
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin4.Connect.MyAddin4")
{
OutputWindow outputWindow = (OutputWindow) _applicationObject.Windows.Item(Constants.vsWindowKindOutput).Object;
OutputWindowPane outputPane = outputWindow.OutputWindowPanes.Add("Processor");
Worker workerObject = new Worker(ref outputPane);
uint loops = 10;
System.Threading.Thread thread = new System.Threading.Thread(delegate() { workerObject.DoWork(loops); });
thread.Start();
handled = true;
return;
}
}
}
private DTE2 _applicationObject;
private AddIn _addInInstance;
}
public class Worker
{
private EnvDTE.OutputWindowPane _pcLintOutputWindowPane;
public Worker(ref EnvDTE.OutputWindowPane pcLintOutputWindowPane)
{
_pcLintOutputWindowPane = pcLintOutputWindowPane;
}
public void DoWork(uint loops)
{
for (int i = 0; i < loops; i++)
{
System.Threading.Thread.Sleep(500);
WriteText(i + "\n");
}
}
private void WriteText(string stringToWrite)
{
_pcLintOutputWindowPane.Activate();
_pcLintOutputWindowPane.OutputString(stringToWrite);
}
}
}
使用系统;
使用可扩展性;
使用EnvDTE;
使用EnvDTE80;
使用Microsoft.VisualStudio.CommandBars;
名称空间MyAddin4
{
公共类连接:IDTExtensibility2、IDTCommandTarget
{
public void OnConnection(对象应用程序、ext_ConnectMode ConnectMode、对象外接程序、ref数组自定义)
{
_applicationObject=(DTE2)应用程序;
_附加值=(附加值)附加值;
if(connectMode==ext\u connectMode.ext\u cm\u ui设置)
{
object[]contextGUIDS=新对象[]{};
Commands2 commands=(Commands2)\u applicationObject.commands;
字符串toolsemuname=“Tools”;
CommandBar菜单栏CommandBar=((CommandBars)_applicationObject.CommandBars)[“菜单栏”];
CommandBarControl toolsControl=menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup=(CommandBarPopup)toolsControl;
尝试
{
Command Command=commands.AddNamedCommand2(_addin实例,“MyAddin4”,“MyAddin4”,“为MyAddin4执行命令”,true,59,ref contextGUIDS,(int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled,(int)vsCommandStyle.vsCommandStylePictAndText,vsCommandControlType.vsCommandControlTypeButton);
如果((命令!=null)&&(工具spopup!=null))
{
command.AddControl(toolsPopup.CommandBar,1);
}
}
public class OutputPane
{
public string Name { get; private set; }
private OutputWindowPane _outputWindowPane;
private static object _outputWindowPaneLock = new object();
public OutputPane(string name)
{
Name = name;
}
public void Write(string text)
{
CreateOutputPane(Name); // Creates the OutputWindowPane if it does not already exist.
if (_outputWindowPane != null)
{
try
{
_outputWindowPane.Activate();
_outputWindowPane.OutputString(text);
}
catch (Exception ex1)
{
System.Diagnostics.Debug.WriteLine("Exception writing text '" + text + "': " + ex1.ToString());
// Exceeded maximum output pane size?
try
{
_outputWindowPane.Clear();
_outputWindowPane.OutputString(text);
}
catch (Exception ex2)
{
System.Diagnostics.Debug.WriteLine("Exception writing text '" + text + "': " + ex2.ToString());
}
}
}
}
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if (commandName == "MyAddin4.Connect.MyAddin4")
{
OutputWindow outputWindow = (OutputWindow)_applicationObject.Windows.Item(Constants.vsWindowKindOutput).Object;
OutputWindowPane outputPane = outputWindow.OutputWindowPanes.Add("Processor");
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(OutputStringThread));
thread.Start(outputPane);
handled = true;
return;
}
}
}
private void OutputStringThread(object obj)
{
try
{
OutputWindowPane outputPane = (OutputWindowPane)obj;
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
outputPane.Activate();
outputPane.OutputString(i + "\n");
}
}
catch (Exception ex)
{
// Handle exception
}
}