写入Visual Studio输出窗口

时间:2017-11-13 14:26:26

标签: c# visual-studio-2015 vsix

我写了一个Visual Studio 2015 Extension做了一些长度的过程。我已经更改了StatusBar,以便显示进度。现在我也希望对Output Window做同样的事情。它有点工作,但问题是当我启动Process Visual Studio完全冻结(我无法点击任何东西)和输出Windows更新一切都完成后。

这是我的代码:

public static readonly Guid ObfuscateWindow = new Guid("30AFCE54-9736-4899-93C5-B59D15510D3B");
IVsOutputWindowPane _outputObfuscatePane;

private Obfuscation(Package package)
{
    if (package == null)
    {
        throw new ArgumentNullException("package");
    }

    this.package = package;

    _dte = Package.GetGlobalService(typeof(SDTE)) as EnvDTE.DTE;
    _statusBar = (IVsStatusbar)ServiceProvider.GetService(typeof(SVsStatusbar));
    CreatePane(ObfuscateWindow, "Verschleierung", true, false);

    OleMenuCommandService commandService = 
    this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

    if (commandService != null)
    {
        var menuCommandID = new CommandID(CommandSet, cmdidObfuscateSln);
        var menuItem = new OleMenuCommand(this.ObfuscateSolutionCallback, menuCommandID);                
        commandService.AddCommand(menuItem);
    }
}

private void CreatePane(Guid paneGuid, string title, bool visible, bool clearWithSolution)
{
    IVsOutputWindow output = (IVsOutputWindow)ServiceProvider.GetService(typeof(SVsOutputWindow));
    output.CreatePane(ref paneGuid, title, Convert.ToInt32(visible), Convert.ToInt32(clearWithSolution));
        output.GetPane(ref paneGuid, out _outputObfuscatePane);
}

private void ObfuscateSolutionCallback(object sender, EventArgs e)
{            
    _dte = Package.GetGlobalService(typeof(SDTE)) as EnvDTE.DTE;
    _statusBar = (IVsStatusbar)ServiceProvider.GetService(typeof(SVsStatusbar));            

    DateTime datStart = DateTime.Now;

    uint cookie = 0;
    object icon = (short)Microsoft.VisualStudio.Shell.Interop.Constants.SBAI_Deploy;

    _statusBar.Progress(ref cookie, 1, "Verschleierung gestartet...", 0, 0);
    _statusBar.Animation(1, ref icon);    

    ObfuscateAssembly(ref _statusBar, ref cookie, _dte, "", ref _outputObfuscatePane);

    _statusBar.Animation(0, ref icon);
    _statusBar.Progress(ref cookie, 0, "Verschleierung abgeschlossen", 0, 0);
}

private void ObfuscateAssembly(ref IVsStatusbar statusBar, ref uint cookie, EnvDTE.DTE dte, string project, ref IVsOutputWindowPane outputPane)
{
    try
    {
       //Declaration removed

        foreach(string f in files)
        {                    
            statusBar.Progress(ref cookie, 1, Path.GetFileName(f), i, totalFileNum);

            strObfuscatedAssemblyFilePath = Path.Combine(strObfuscatedAssemblyDirectory, "Bereit");

            CreateObfuscatorProjectFileFromTemplateFile(f, strConfuserExProjectTemplateFilePath, strObfuscatedAssemblyDirectory, strConfuserExProjectFilePath);
            ObfuscateProjectAssembly(strObfuscatedAssemblyFilePath, strConfuserExFilePath, strConfuserExProjectFilePath, ref outputPane);
            i++;                  
        }
    }
    catch (Exception ee)
    {
        Console.WriteLine(ee.ToString());
    }
}

private void ObfuscateProjectAssembly(string strObfuscatedAssemblyFilePath, string strConfuserExFilePath, string strConfuserExProjectFilePath, ref IVsOutputWindowPane outputPane)
{
    try
    {
        if (File.Exists(strObfuscatedAssemblyFilePath)) File.Delete(strObfuscatedAssemblyFilePath);
        using (PowerShell PowerShellInstance = PowerShell.Create())
        {
            PowerShellInstance.AddScript("& \"" + strConfuserExFilePath + "\" -nopause \"" + strConfuserExProjectFilePath + "\"");
            Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

            if (PowerShellInstance.Streams.Error.Count > 0)
            {
                foreach (ErrorRecord er in PowerShellInstance.Streams.Error)
                {

                }
            }

            foreach (PSObject outputItem in PSOutput)
            {
                if (outputItem != null)
                {
                    //System.Diagnostics.Debug.Write(outputItem.BaseObject.ToString() + "\n");     
                    outputPane.OutputString(outputItem.BaseObject.ToString() + "\n");                       
                }
            }
        }
    }
    catch (Exception ee)
    {
        Console.WriteLine(ee.ToString());
    }
}

0 个答案:

没有答案