Do you know how to pause a MS Build task? Here I’d like to give you an option by writing a simple task which can be included in a project file.
First create a new C# Class Library Solution called “SleepTask”, Copy & Paste the following C# code and compile the entire solution. But don’t forget to add the Microsoft.Build.Framework and Microsoft.Build.Utilities references in your project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using System; using System.Threading; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; namespace SleepTask { public class Sleep : Task { public override bool Execute() { Thread.Sleep(Timeout); return true; } private Int32 timeout; public Int32 Timeout { get { return timeout; } set { timeout = value; } } } } |
Now write a new MS Build project file (e.g. Project.xml) and Copy & Paste the following XML tags. With the “Timeout” attribute you can define the timeout in milliseconds for the SleepTask (e.g. 3 seconds). Before running the project you have to copy the SleepTask.dll assembly into the folder of your Project.xml file, of course.
1 2 3 4 5 6 7 | <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Default"> <UsingTask AssemblyFile="SleepTask.dll" TaskName="Sleep" /> <Target Name="Default"> <Sleep Timeout="3000" /> </Target> </Project> |
Finally run your MS Build task by typing the following command (use the Visual Studio Command Prompt) and see that the task is paused because of the defined timeout.
MSBuild Project.xml







0 Responses to “MSBuild Sleep Task”