0

One of my projects has Exec Copy command to copy a file from one directory to another. We are intermittently seeing an issue where one of the process is locking a file and MsBuild is trying to execute a copy command on the same file. So, we are facing the error that the file is in use by another process.

We are suspecting that this is because of two different elements executing in parallel. Unless the first one finishes we do not want the second one to execute. Can we have two values for AfterBuild attribute like below.

Below are the projects: Project1.proj and Project2.proj. I want to use the target defined in Project2 into Project1. Project1.proj has a reference added for Projet2.proj

Project1.proj

<Target Name="TestTarget" AfterTargets="Build;TestTargetAnother" > ..... ..... </Target>

Project2.proj

<Target Name="TestTargetAnother" AfterTargets="Build" > ..... ..... </Target>

om dev
  • 1
  • 2
  • Did you want to make one of the two targets execute and when one executes, disable the other? – Mr Qian Sep 04 '20 at 06:24
  • Hi om dev, any update about this issue? Please check if the answer helps you handle the issue? – Mr Qian Sep 07 '20 at 02:25
  • Hi @PerryQian-MSFT, These project files have multiple other elements, so I couldn't find a way to make them .targets files and test your changes. – om dev Sep 08 '20 at 06:54
  • You can first enter your project's csproj file, and then find any import node which is your reference files' path. Then change them to `.targets`. You just change the reference proj files to targets. targets is more normalized. – Mr Qian Sep 08 '20 at 06:58
  • And progress about it? – Mr Qian Sep 09 '20 at 02:51

1 Answers1

0

We are suspecting that this is because of two different elements executing in parallel. Unless the first one finishes we do not want the second one to execute.

First, rename your two files as Project1.targets and Project2.targets.

The file with proj suffix cannot be used in other referenced files. Files with this suffix cannot transfer properties.

Modify Project1.targets like this:

<Project>

    <PropertyGroup>
        <Flag>false</Flag>
    </PropertyGroup>


    <Target Name="TestTarget" AfterTargets="Build;TestTargetAnother" Condition="'$(Flag)'=='false'">

        <Exec Command="xxxxxxxxxxx"/>

        <PropertyGroup>
            <Flag>true</Flag>
        </PropertyGroup>

    </Target>

    <Import Project="Project2.targets"/>

</Project>

Then, modify Project2.targets like this:

<Project>
    
    
<Target Name="TestTargetAnother" AfterTargets="Build" Condition="'$(Flag)'=='false'">
<Exec Command="xxxxxxxx"/>
<PropertyGroup>
     <Flag>true</Flag>
</PropertyGroup>
</Target>
</Project>

After that, reference Project1.targets into your xxx.csproj file:

 <Import Project="Project1.targets" />

So it will execute one of them and cancel the other.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41