简体   繁体   中英

Output stream of text to VS2010 Output Window

I've created an addin for VS2010 that uses a separate class to do some processing. The result of this is text that is displayed in a new pane in the Output Window.

The constructor of this processing class takes a reference to a new OutputWindowPane which is created by the addins Connect class. Text is written using the OutputString method.

This works and text is displayed correctly, however, it is all displayed in one update when execution returns to addins Connect class. I also notice that the IDE seems to freeze while the addin is running. I'm new to addin development, so have I missed something obvious?

Is there a way I can modify this process to have the text update in the Output Window with each call of OutputString? I was hoping for an update method of some kind, but I've been unable to find it.

Update

I noticed that this has nothing to do with using a separate class. The following example illustrates my problem:

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;
    }
}

Solution

Building on the answer from @ShellShock this is the solution that works for me:

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);
      }
   }
}

This is what I use to write to an OutputWindowPane in my addin, and it updates the text for each call. I'm using VS2008, I don't know if it works differently in VS2010.

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());
                }
            }
        }
    }

Edit: I think you also need to call OutputWindowPane.OutputString on a background thread, which is what I do when I call my OutputPane.Write method. So using your code, it will be something like this:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM