Visual Studio 2010 中预构建步骤中的吞咽错误

发布于 2024-11-03 23:13:36 字数 285 浏览 4 评论 0原文

我的解决方案有很多项目,其中一个是 Windows 服务;我有一个预构建步骤来停止服务,还有一个后构建步骤来重新启动它(这样,当 VS 构建/覆盖它时,Windows 服务 exe 不会被锁定)。

on pre-build: 
net stop myservice

on post-build:
net start myservice

如果在我开始构建时该服务未运行,则 net stop 命令将失败并阻止构建继续进行。

即使预构建步骤失败,我该怎么做才能仍然强制构建?

My solution has a bunch of projects one of which is a windows service; I have a prebuild step to stop the service and a postbuild step to restart it (this way the windows service exe is not locked when VS is building/overwriting it).

on pre-build: 
net stop myservice

on post-build:
net start myservice

If the service is not running while I'm starting the build, the net stop command fails and that prevents the build from proceeding.

What can I do to still force the build even if the pre-build step failed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

枯叶蝶 2024-11-10 23:13:37

我想通了 - 你只需要在末尾添加以下语句:

SET ERRORLEVEL = 0

或者简单地:

EXIT 0

I figured it out - you simply need to add the following statement at the end:

SET ERRORLEVEL = 0

or simply:

EXIT 0
假扮的天使 2024-11-10 23:13:37

我知道这是一篇旧帖子,但我最近也遇到了这个问题。我想终止我正在构建的进程(如果它当前正在运行),并发现我可以执行:

taskkill /f /im $(TargetName).exe 2>nul 1>nul
Exit 0

2>nul 1>nul 而无需 Exit 0,反之亦然,似乎不起作用。我必须两者都做。

另外值得注意的是,这是使用 Visual Studio Express 2012。

我在研究这个问题时也找到了解决方案 在此博客上

2>nul 1>nul 将从命令中吞掉 stderr 和 stdout。 EXIT 0 将确保构建事件返回 0。

I know this is an old post, but I recently had this issue as well. I wanted to kill the process that I was building if it was currently running, and found that I could do:

taskkill /f /im $(TargetName).exe 2>nul 1>nul
Exit 0

2>nul 1>nul without Exit 0, or vice versa, doesn't seem to work. I have to do both.

Also worth noting this is using Visual Studio Express 2012.

I found the solution when looking into this issue as well on this blog

The 2>nul 1>nul will swallow the stderr and stdout from the command. The EXIT 0 will make sure the build event returns 0.

懵少女 2024-11-10 23:13:37

不需要重定向 stdout,只需重定向 stderr。此外,如果需要,set errorlevel 允许您稍后添加额外的行。退出将立即终止。

taskkill /f /im:$(TargetFileName) 2>nul &set errorlevel=0

适用于 VS 2017。

There is no need to redirrect stdout, just stderr. Also, set errorlevel allows you to have extra lines later if you need. Exit will terminate immediately.

taskkill /f /im:$(TargetFileName) 2>nul &set errorlevel=0

Works in VS 2017.

墟烟 2024-11-10 23:13:37

有点晚了,但这是一个很好的解决方案,所以无论如何我都会分享它。

以下解决方案取自此 博客文章

您只需根据需要定义您的例如构建后步骤。您不需要将它们包装到批处理文件中,但您可以。然后,更改项目文件(例如 vbproj 或 csproj 文件)并在项目结束标记之前插入以下片段:

<Target
  Name="PostBuildEvent"
  Condition="'$(PostBuildEvent)'!=''"
  DependsOnTargets="$(PostBuildEventDependsOn)">
  <Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" ContinueOnError="true" />
</Target>

这会更改 Visual Studio 执行构建后步骤的方式。 ContinueOnError 属性指示 VS 简单地忽略错误。它仍然在构建结果中发出警告,但不会停止构建。

您可以通过相应地更改上述片段来对 PreBuildEvent 执行相同的操作。

当然,这仅适用于完整的所有构建步骤。您不能选择性地仅忽略某些步骤上的错误。如果您需要这样做,则必须坚持使用批处理文件的解决方案,但上述解决方案的好处是您不需要单独的批处理。

萨沙

A bit late, but a good solution, so I'll share it anyway.

The following solution was taken from this blog post.

You simply define your e.g. post build steps as needed. You don't need to wrap them into a batch file, but you could. Then you alter you project file (such as a vbproj or csproj file) and insert the following fragment just before the project closing-tag:

<Target
  Name="PostBuildEvent"
  Condition="'$(PostBuildEvent)'!=''"
  DependsOnTargets="$(PostBuildEventDependsOn)">
  <Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" ContinueOnError="true" />
</Target>

This changes the way visual studio executes the post build steps. The ContinueOnError Attributes instructs VS to simply ignore errors. It still issues warnings in the build results, but it does not stop the build.

You can do the same for the PreBuildEvent by changing the above fragment accordingly.

Of course this works only for all of the build steps in full. You can not selectivly ignore errors on certain steps only. If you need to do that you have to stick to the solution with the batch file, but the nice thing about the solution above is you don't need a separate batch.

Sascha

夜灵血窟げ 2024-11-10 23:13:37

net 命令封装在批处理文件中并使用 exit /B 0

http://ss64.com/nt/exit.html

Wrap your net commands in a batch file and use exit /B 0

http://ss64.com/nt/exit.html

递刀给你 2024-11-10 23:13:37

将命令嵌入到始终返回 0 的可执行文件中是否可以解决您的问题?

在c调用中

system("net stop myservice")

Would embedding the command in an executable that always returns 0 solve your issue?

in c call

system("net stop myservice")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文