如何从VisualStudio生成后事件中启动后台任务? [英] How do I start a background task from a VisualStudio post-build event?

查看:508
本文介绍了如何从VisualStudio生成后事件中启动后台任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论何时有特定项目,我都会尝试启动 grunt watch 的实例.从VisualStudio运行.我有一个用ember编写的页面应用程序,并用grunt编译,该应用程序连接到用WebAPI编写的后端.我正在尝试减少摩擦,以便可以在VS中启动调试(F5),并使它正常运行.

I'm trying to start an instance of grunt watch whenever a particular project is run from VisualStudio. I have a single page-app written in ember, and compiled with grunt, which connects to a backend written with WebAPI. I'm trying to reduce friction so it's possible to Start Debugging (F5) in VS and have it get everything up and going.

如果我添加了一个构建后事件来用grunt编译应用程序,它将很好地工作:

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

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

grunt watch永远不会终止,因此VisualStudio构建似乎会挂起,直到您终止node.exe进程(通常是预期的,除了无法使用Ctrl + Break在VS中停止之外):

grunt watch never terminates, so the VisualStudio build appears to hang until you terminate the node.exe process (which is mostly expected, other than not being able to use Ctrl+Break to stop in VS):

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

我尝试从start命令开始,但是VisualStudio仍在等待它退出(只是说构建已开始..."):

I've tried starting with the start command, but VisualStudio still waits for it to exit (just saying "Build started..."):

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

我也尝试过使用start的/i参数,但这无济于事.它会打开一个运行grunt watch的新窗口,但是直到关闭控制台窗口,构建过程才能继续(并且该应用程序无法启动).

I've also tried with start's /i parameter, but that doesn't help. It opens a new window running grunt watch, but the build doesn't continue (and the app doesn't start) until I close the console window.

大概与生成过程的子过程有关吗?有没有Visual Studio等待的实际方法来启动后台任务?

Presumably this has something to do with the process being a child of the build process? Is there an actual way to start a background task without VisualStudio waiting on it?

推荐答案

不确定确切启动为什么没有效果(从命令行完美运行),但是afaik msbuild为其生成事件生成了单独的cmd进程,因此与此有关.

Not sure why exactly start doesn't do the trick (works perfectly from the command line), but afaik msbuild spawns a seperate cmd process for it's build events so it will have something to do with that.

一个可行的替代方法是使用Powershell来启动该过程.不知道内置的powershell语法,但是调用C#的Process.Start可以很好地工作:

A working alternative is to use Powershell to start the process instead. No idea about the builtin powershell syntax, but invoking C#'s Process.Start works just as fine:

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

这回答了您的问题,但是我认为这不是您真正想要的,而您问的是错误的问题.来自VisualStudio",但随后您继续询问有关 build 项目时发生的构建事件.这是不同的,并且似乎不方便,因为每个单独的构建都将启动一个新实例.相反,我认为您真正想要/需要的是每次您开始调试项目时都启动一个实例.在此处:

That answers your question, however I think it's not what you really want, and you're asking the wrong question.. You say you want to 'start an instance whenever a particular project is run from VisualStudio', but then you go on asking about build events which occur when a project is built. That is different and seems unhandy since every single build will start a new instance. Instead, I think what you actually want/need is to start an instance everytime you start debugging your project. That's also possible as laid out here:

  • 将一个空项目添加到您的解决方案中
  • 在项目的Properties->Debugging
  • 下输入节点命令
  • 右键单击解决方案,选择Set Startup Project
  • 选择Multiple startup projects
  • 为您的主要项目将Action设置为Start
  • 将空项目的Action设置为Start without debugging
  • add an empty project to your solution
  • enter your node command under the projects' Properties->Debugging
  • right-click solution, select Set Startup Project
  • select Multiple startup projects
  • set Action to Start for your main project
  • set Action to Start without debugging for the empty project

现在按F5键,VS将启动节点,并开始调试您的项目.

Now hit F5 and VS will start node, plus start debugging your project.

这篇关于如何从VisualStudio生成后事件中启动后台任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆