How to start a background job from an event after building VisualStudio?

I try to start an instance of grunt watch whenever a particular project is started from VisualStudio. I have a one-page application written in ember and compiled with grunt that connects to a backend written using WebAPI. I am trying to reduce friction so that I can start debugging (F5) in VS and get everything I need.

If I add a post-build event to compile the application using grunt, it works fine:

node node_modules\grunt-cli\bin\grunt dev --no-color 

grunt watch never ends, so a VisualStudio assembly is created until you finish the node.exe process (which is mostly expected, except that it cannot use Ctrl + Break to stop in VS):

 node ../node_modules\grunt-cli\bin\grunt watch --no-color 

I tried to start with the start command, but VisualStudio is still waiting for it to exit (just saying "Build started ..."):

 start node ../node_modules\grunt-cli\bin\grunt dev --no-color 

I also tried with the start /i option, but that does not help. It opens a new window with grunt watch , but the assembly does not continue (and the application does not start) until I close the console window.

enter image description here

Presumably this is because the process is a child of the build process? Is there a real way to run a background task without waiting for VisualStudio?

+6
source share
1 answer

Not sure why the beginning doesn’t do the trick (works fine from the command line), but afaik msbuild spawns a separate cmd process for its build events, so it will have something to do with it.

A working option is to use Powershell to start the process. I don’t know what the powershell built-in syntax is, but when starting C # Process.Start works just as well:

 powershell -Command "[System.Diagnostics.Process]::Start( '/path/to/node', 'args' )" 

This answers your question, however, I think that this is not what you really want, and you are asking the wrong question. You say you want to "start an instance whenever a particular project starts from VisualStudio", but then you continue to ask about build events that occur when the project is built. This is different and seems unmanageable as each individual assembly starts a new instance. Instead, I think you really want / need to run an instance every time you start debugging your project. It is also possible, as laid out here :

  • add an empty project to your solution
  • enter the node command as part of the projects Properties->Debugging
  • right click select Set Startup Project
  • select Multiple startup projects
  • set Action to Start for your main project
  • set Action to Start without debugging for an empty project

Now press F5, and VS starts node, and also starts debugging your project.

+6
source

Source: https://habr.com/ru/post/952615/


All Articles