MSBuild - 等待 x 秒 [英] MSBuild - wait for x seconds

查看:62
本文介绍了MSBuild - 等待 x 秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部署 Web 应用程序的 MSBuild 脚本.它通过将app_offline.htm"文件复制到服务器来停止当前的 Web 应用程序.之后它会删除其他文件,然后将新文件复制到服务器.

I have a MSBuild script that deploys a web app. It stops the current web app by copying a 'app_offline.htm' file to the server. After this it deletes other files then copies new ones to the server.

我需要在app_offline.htm"的复制和删除之间添加一个延迟.

I need to add a delay between the copying of 'app_offline.htm' and the delete.

我当前的脚本会引发错误,因为当脚本尝试删除文件时,文件仍处于锁定状态.

My current script throws errors as the files are still locked when the script tries to delete them.

在 MSBuild 中执行此操作的最佳方法是什么?

What is the best way to do this in MSBuild?

我的停止任务看起来像这样...

My Stop task looks like this...

  <Target Name="Stop">
    <WriteLinesToFile File="$(DeployDirectory)\app_offline.htm" Lines="Offline for maintenance" Overwrite="true" Encoding="Unicode"/>
  </Target>

我的删除任务看起来像这样...

My Delete task looks like this...

  <Target Name="Clean">
    <ItemGroup>
      <Files Include="$(DeployDirectory)\**\*" Exclude="$(DeployDirectory)\app_offline.htm" />
      <Files Include="$(LogDirectory)\*" />
    </ItemGroup>
    <Delete Files="@(Files)" />
  </Target>

推荐答案

有多种选择:

  1. MSBuild Community Tasks 具有内置的 Sleep 任务.你可以这样使用它:

  1. MSBuild Community Tasks has built-in Sleep task. You can use it like this:

<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />
<Sleep Milliseconds="300" />

  • MSBuild 扩展包 还包含内置的 Thread 任务它提供睡眠功能.使用示例 在这里.

  • MSBuild Extension Pack also contains built-in Thread task which offers Sleep functionality. Example of usage is here.

    当然,任何类型的 cmd 兼容等待也可以通过 Exec 命令工作:

    Of course any kind of cmd-compatible wait can work as well via Exec command:

    • 已经提到了<Exec Command="ping -n 6 127.0.0.1 > nul"/>
    • 我也有 <Exec Command="sleep 5"/> 很好地为我工作.
    • Already mentioned <Exec Command="ping -n 6 127.0.0.1 > nul" />
    • I also had <Exec Command="sleep 5" /> worked for me nicely.

    您可以为此定义 MSBuild 内联 C# 任务:

    You can define MSBuild inline C# task for that purpose:

    <UsingTask TaskName="Sleep" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
      <ParameterGroup>
        <!-- Delay in milliseconds -->
        <Delay ParameterType="System.Int32" Required="true" />
      </ParameterGroup>
      <Task>
        <Code Type="Fragment" Language="cs">
          <![CDATA[
    System.Threading.Thread.Sleep(this.Delay);
    ]]>
        </Code>
      </Task>
    </UsingTask>
    
    ...
    
    <Sleep Delay="5000"/>
    

  • 这篇关于MSBuild - 等待 x 秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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