How to cancel the build process in VisualStudio for a long custom build tool?

I have a custom build step. At this point, I have a great long tool for converting a database into a special static compressed format. This tool processes almost 2 GB of data and works up to 30 minutes.

Tools handle Ctrl + C and Ctrl + Break. And you can easily stop it when starting from the command line.

But inside VisualStudio (I am currently using VS2015) I can not stop this tool to start. The compiler and linker stop when I select Build -> Cancel. But my tool continues to work.

This means that VS does not process the interrupt signal for the processes that it starts.

Is there any trick or option to undo tools during the build process without using the task manager?

+4
source share
1 answer

The compiler and linker "know" to stop when you cancel the assembly because they are executed as MSBuild "Task Task" and are sensitive to the ToolCanceled event.
Although you cannot do the same with a custom build step, you can wrap your tool in a class that extends ToolTask and kills your tool when ToolCanceled .

msbuild. <Code Type="class" ...

ToolTask ICancelableTask, Cancel() , .
Cancel() MSBuild, .

, Cancel():

 <UsingTask TaskName="MyTool" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">

    <ParameterGroup> 
      <!-- Tool parameters -->     
    </ParameterGroup>

    <Task>
      <Code Type="Class" Language="cs">
        <![CDATA[  

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

      public class MyTool : Task, ICancelableTask
      {
         public override bool Execute()
         {
           Log.LogMessage(MessageImportance.High, "MyTool: Build started!");

           // Code to run your tool...
         }

         public void Cancel()
         {         
           Log.LogMessage(MessageImportance.High, "MyTool: Build cancelled!");

           // Code to stop your tool...          
         }
      }

       ]]>
      </Code>
    </Task>
  </UsingTask>
+1

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


All Articles