0

我为 VS2010 创建了一个插件,它使用一个单独的类来进行一些处理。其结果是在输出窗口的新窗格中显示的文本。

此处理类的构造函数引用由插件 Connect 类创建的新 OutputWindowPane。文本是使用 OutputString 方法编写的。

这有效并且文本显示正确,但是,当执行返回到插件连接类时,它全部显示在一次更新中。我还注意到,在插件运行时 IDE 似乎冻结了。我是插件开发的新手,所以我错过了一些明显的东西吗?

有没有办法可以修改此过程以在每次调用 OutputString 时更新输出窗口中的文本?我希望有某种更新方法,但我一直找不到。

更新

我注意到这与使用单独的类无关。以下示例说明了我的问题:

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

解决方案

基于@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);
      }
   }
}
4

1 回答 1

1

这是我用来写入插件中的 OutputWindowPane 的内容,它会更新每次调用的文本。我正在使用VS2008,我不知道它在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());
                }
            }
        }
    }

编辑:我认为您还需要在后台线程上调用 OutputWindowPane.OutputString,这是我在调用 OutputPane.Write 方法时所做的。因此,使用您的代码,它将是这样的:

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
    }
}
于 2012-10-02T14:03:29.850 回答