DTE.ExecuteCommand并等待

问题描述 投票:8回答:2

我想使用宏来发布我的Web应用程序项目。小问题是,DTE.ExecuteCommand异步运行,我需要等到命令完成。

例:

    DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
    DTE.ActiveWindow.Object.GetItem("04 - Products\04 - Products.WSS").Select(vsUISelectionType.vsUISelectionTypeSelect)
    DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Publish")
    '// now I want copy (and overwrite) some files, but AFTER the publish

是否有一些同步对象或有关已执行命令状态的信息?

c# visual-studio automation macros envdte
2个回答
5
投票

嘿伙计们,如果你还在寻找这个问题的答案,试试吧。

绑定到发布事件并成功推送您的外部命令。我正在做类似的事情来构建解决方案然后解雇MSpec测试运行器(blog post)。

为此,您需要为PublishEvents_OnPublishDone添加一个钩子。通过转到EnvironmentEvents模块并添加以下内容来执行此操作:

<System.contextStaticAttribute()> Public WithEvents PublishEvents As EnvDTE80.PublishEvents

Private Sub PublishEvents_OnPublishDone(ByVal Success As Boolean) Handles PublishEvents.OnPublishDone
    'call custom module sub here.
End Sub

如果你只想运行外部命令,有时候会做这样的事情。像这样创建你的宏:

Public runExternalCommandOnComplete As Boolean = False

Sub PublishAndRunExternalCommand()

    DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
    DTE.ActiveWindow.Object.GetItem("04 - Products\04 - Products.WSS").Select(vsUISelectionType.vsUISelectionTypeSelect)
    DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Publish")

    runExternalCommandOnComplete = True

End Sub

然后在EnvironmentEvents中添加:(注意:CustomMacros是您将代码放在上面的模块的名称)

<System.contextStaticAttribute()> Public WithEvents PublishEvents As EnvDTE80.PublishEvents

Private Sub PublishEvents_OnPublishDone(ByVal Success As Boolean) Handles PublishEvents.OnPublishDone
   CustomMacros.runExternalCommandOnComplete = False
   'Where ExternalCommand1 matches the command you want to run
   DTE.ExecuteCommand("Tools.ExternalCommand1")  
End Sub

应该这样做。

干杯,

凯尔


2
投票

以下是编译单个文件的方法,然后链接整个解决方案,例如:

Dim WithEvents t As Timers.Timer

Sub test()
    DTE.ExecuteCommand("Build.Compile")
    t = New Timers.Timer
    t.Interval = 0.05
    t.Start()
End Sub

Sub t_Elapsed(ByVal ee As Object, ByVal dd As Timers.ElapsedEventArgs) Handles t.Elapsed

    If DTE.Solution.SolutionBuild.BuildState <> vsBuildState.vsBuildStateInProgress Then
        t.Stop()
        DTE.ExecuteCommand("Build.Link")
    End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.