- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在使用以下代码成功实例化/自动化 Visual Studio:
System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.9.0");
object obj = Activator.CreateInstance(t, true);
dte = (DTE)obj;
Solution sln = dte.Solution;
sln.Open(SolutionFile);
System.Threading.Thread.Sleep(1000);
//Do stuff with the solution
注意到 Thread.Sleep(1000)
调用了吗?如果我不包括它,代码会尝试在实例准备就绪之前对其进行调试,我会得到一个异常:
the message filter indicated that the application is busy.
有没有办法轮询此对象是否就绪,而不是正好等待 n 秒?
最佳答案
作为此问题的解决方案,您可以注册一个事件,该事件会在解决方案加载完成时发出通知。
这是一个类示例,可让您在解决方案加载时收听事件:
public class SolutionEventsListener : IVsSolutionEvents, IDisposable
{
private IVsSolution solution;
private uint solutionEventsCookie;
public event Action AfterSolutionLoaded;
public event Action BeforeSolutionClosed;
public SolutionEventsListener(IServiceProvider serviceProvider)
{
InitNullEvents();
solution = serviceProvider.GetService(typeof (SVsSolution)) as IVsSolution;
if (solution != null)
{
solution.AdviseSolutionEvents(this, out solutionEventsCookie);
}
}
private void InitNullEvents()
{
AfterSolutionLoaded += () => { };
BeforeSolutionClosed += () => { };
}
#region IVsSolutionEvents Members
int IVsSolutionEvents.OnAfterCloseSolution(object pUnkReserved)
{
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
{
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
{
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
{
AfterSolutionLoaded();
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
{
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnBeforeCloseSolution(object pUnkReserved)
{
BeforeSolutionClosed();
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
{
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel)
{
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnQueryCloseSolution(object pUnkReserved, ref int pfCancel)
{
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
{
return VSConstants.S_OK;
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (solution != null && solutionEventsCookie != 0)
{
GC.SuppressFinalize(this);
solution.UnadviseSolutionEvents(solutionEventsCookie);
AfterSolutionLoaded = null;
BeforeSolutionClosed = null;
solutionEventsCookie = 0;
solution = null;
}
}
#endregion
}
使用示例:
DTE2 applicationObject = dte;
var serviceProvider = new ServiceProvider(applicationObject as IServiceProvider);
solutionEventsListener = new SolutionEventsListener(serviceProvider);
solutionEventsListener.AfterSolutionLoaded += () => /* logic here */ ;
关于c# - 使用 EnvDTE 自动化 Visual Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2525457/
我有一个当前遵循的 Vs2010 包 EnvDTE=>Solution=>Projects=>CodeModel=>CodeElements 递归地执行以下操作并找到类 var q = element
作为 NuGet 安装的一部分,我们需要在安装完成时打开一个网页。 我们的要求是: 如果网页尚未在另一个窗口中打开,请在新窗口中的 Visual Studio 中打开该网页。 自己打开网页很简单: .
我在 Visual Studio 2012 上使用 C# 编写了一个 Visual Studio 向导模板。 我遵循了 MSDN 的步骤:我创建了一个 VS 模板,然后我创建了一个带有实现 IWiza
在 VS 解决方案中有以下结构: 现在,我想获取对“Catalogs”文件夹的引用并将其存储在变量中: //Solution_Name // Configurat
我试图通过 Powershell (envdte) 创建一个带有嵌套解决方案文件夹的 Visual Studio 解决方案。一切都工作到 1 级深,但嵌套解决方案文件夹似乎不起作用(接口(interf
是否可以使用 EnvDTE在非扩展项目(例如类库项目)中组装? 如果是这样,我将如何访问 the ConfigurationManager.ActiveConfiguration property ?
我一直在关注 MSDN's Hello World guide to developing Visual Studio extensions (本文专门讨论如何将其创建为 Visual Studio
我试图在事件文档中找到一个函数,但是我遇到了一个问题,当函数在命名空间内时,我的代码试图获取 EnvDTE.CodeNamespace 对象的成员,但它返回空,我也尝试过 Children,但它也是空
我想使用 ENVDTE 添加对项目的服务引用。唯一的方法是通过以下命令弹出添加服务引用窗口: _applicationObject.ExecuteCommand("Project.AddService
代码语句是指最小的独立代码元素。是否有一个接口(interface)可以以结构化形式获取 EnvDTE.CodeFunction 体内的代码语句。 有一些方法可以将 EnvDTE.CodeFuncti
需要将项目从Solution 移动到SolutionFolder。我正在尝试从解决方案中删除项目并使用 EnvDTE SolutionFolder.AddFromFile(Filepath line)
我已经创建了一个 Visual Studio 加载项,可以将表单添加到打开的解决方案中的现有项目。 这是我创建表单的方式: string templatePath = sol.GetProjectIt
我已经使用 EnvDTE 列出了我的解决方案中的所有项目,但我在我的代码中发现了一个错误:我无法获取已卸载的项目。 我找到了一种跳过卸载项目的方法: if (string.Compare(EnvDTE
我在使用 EnvDTE 创建一种方法来列出我的项目中的所有类时遇到问题,以便使用 T4(基于命名约定)对接口(interface)进行模板化,并且那里的文档似乎都没有描述怎么做。我开始于:
我正在编写一些设计时代码。我想使用这个片段:(找到 here) var dte = (EnvDTE.DTE) GetService(typeof(EnvDTE.DTE)); if (dte != nu
我在 PowerShell 脚本中使用 EnvDTE 来自动化 Visual Studio 2010。这是我使用的代码片段: [void][System.Reflection.Assembly]::L
我使用以下代码将新的 json 文本分配给文档: public virtual async Task SetDocumentText(FileInfo fileInfo, string con
我正在努力跟上 T4 模板的速度。我找到了以下示例 ( here): EnvDTE 命名空间中的任何类型都无法识别。我正在使用 Visual T4 扩展。所有 EnvDTE 类型都带有红色
我正在使用 EnvDTE 在我的最新项目中生成一些代码。 我有一个给定 C# 类的代码类对象的引用,但现在我想遍历它的所有成员(在 codeClass.Members 中)并检查它们的类型。 但是,我
我正在创建一个脚手架工具,并希望通过 EnvDTE 扩展在某个项目中安装 nuget 包。这可能吗? 最佳答案 是的。使用 Invoking NuGet Services from inside Vi
我是一名优秀的程序员,十分优秀!