MSBuild: How to check if a process exists?

Is it possible to write a condition in msbuild that checks if a specific process exists? Or, alternatively, does anyone know about such a task?

Today, my process creates a pid file, the existence of which I am checking. But I do not like all the extra services associated with such a file.

Any ideas?

+3
source share
1 answer

There is no such task in the MSBuild Extension Pack or in the MSBuild Community Tasks . But you can easily create one. Something like that:

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 this:

<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>
+5
source

Source: https://habr.com/ru/post/1705021/


All Articles