tags:

views:

224

answers:

1

Howdy. Is it possible to write a Condition in msbuild that checks if a certain process exists? Or, alternatively, does anyone know of such a task?

Today, my process creates a pid file, which existence I check. But I do not like all the extra maintenance involved with such a file.

Any ideas?

+1  A: 

There is no such task in MSBuild Extension Pack or in MSBuild Community Tasks. But you could easily create a such one. Something like this:

using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace StackOverflow.MSBuild
{
  public class IsProcessRunning : Task
  {
    private string processName;
    private bool isRunning;

    [Required]
    public string ProcessName
    {
      get { return processName; }
      set { processName = value; }
    }

    [Output]
    public bool IsRunning
    {
      get { return isRunning; }
    }

    public override bool Execute()
    {
      if(string.IsNullOrEmpty(processName))
      {
        Log.LogError("ProcessName could not be empty");
        return false;
      }

      foreach(Process clsProcess in Process.GetProcesses())
      {
        if(clsProcess.ProcessName.Contains(processName))
        {
          isRunning = true;
        }
      }

      return true;
    }
  }
}

And you use it like that:

<UsingTask AssemblyFile="$(Task_Assembly_path)"
         TaskName="StackOverflow.MSBuild.IsProcessRunning" />

<Target Name="TestTask">
  <IsProcessRunning ProcessName="${Process}">
    <Output ItemName="Result" TaskParameter="IsRunning"/>
  </IsProcessRunning>

  <Message Text="Process ${Process} is running"
           Condition="'${Result}' == 'true'"/>
</Target>
madgnome
I'd suggest using Process.GetProcessesByName(<name>). Also, in C# 3.0 you can use trivial property declarations, which cuts down on clutter, e.g.: public string "ProcessName { get; set; }" "public bool IsRunning { get; private set; }"
Wedge