|
MSBuild is great for automated batch processes. It's easy to set properties in the script itself using the <PropertyGroup>. However, MSBuild is powerful enough that you can also collect user input with a WinForm.
Why would you do this, given that the whole point of a build process is to be automated? ... Because MSBuild is an automation language, it can be used not just for builds but to
ultimately automate any task. For some tasks that you kick off manually (like installations, deployments, instrumentation, etc...) you may want to collect the user input up front.
Doing this requires three steps.
The first step is easy, lets look at the second one. There are several different ways we can return error information from a WinForm.
With this technique, everything becomes easy: Our MSBuild script can be simple:
<Target Name="EndPoint">
<!-- First compile project that contains WinUI -->
<MSBuild Projects="WindowsApplication1\WindowsApplication1.csproj"
Targets="Build"
Properties="Configuration=Debug" />
<Message Text="Start UI" />
<Exec WorkingDirectory="WindowsApplication1\bin\Debug" Command="WindowsApplication1.exe" />
<Message Text="Done, do other stuff now..." />
</Target>
The WinForm is easy:
static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static int Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); return _intReturnCode; } private static int _intReturnCode = 0; public static int ReturnCode { get { return _intReturnCode; } set { _intReturnCode = value; } } }
We can then set the static variable "ReturnCode" anywhere in the WinForm by setting Program.ReturnCode.
(Note that we can also have the WinForm use Console.Writeline(), which will be logged in the MSBuild script).
Having the MSBuild script take user input is an easy trick to perform, and may be helpful for certain tasks.