DTE.ExecuteCommand and wait

TcKs picture TcKs · Jun 16, 2009 · Viewed 6.9k times · Source

I would like use macros for publishing my webapplication project. The little problem is, DTE.ExecuteCommand run asynchronously, and I need to wait until the command is done.

Example:

    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

Is there some synchronization object or information about state of executed command?

Answer

K Finley picture K Finley · Oct 6, 2009

If you're still looking for an answer to this one, try this.

Tie into the publish events and on a successful push call your external command. I'm doing a similar thing with building the solution and then firing the MSpec test runner (blog post).

To do this you need to add in a hook for PublishEvents_OnPublishDone. Do this by going to the EnvironmentEvents Module and addin the following:

<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

If you only want to run the external command sometimes do something like this. Create your macro like this:

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

Then in EnvironmentEvents add this: (Note: CustomMacros is the name of the module where you put the code above)

<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

That should do it.