Denis
Denis

Reputation: 41

Run parallel build of Microsoft.Build.Evaluation.Project from C#

Good morning. Please help me with msbuild.

I have many loaded csproj in array of Microsoft.Build.Evaluation.Project. I changed same properties in projects. How run parallel build of my projects?

When I run project.Build() in many threads, I received exception "The operation cannot be completed because a build is already in progress.".

I can't save projects on disk, becouse I change property, that need only for me.

Upvotes: 4

Views: 1260

Answers (1)

ekarankow
ekarankow

Reputation: 329

You can call only one Build() function per process. So you need to synchronize all Build() call. Something like that:

static class ProjectBuildSync
{
    private static readonly object _syncObject = new object();
    public static bool Build(Project project)
    {
        lock (_syncObject)
            return project.Build();
    }
}

...
ProjectBuildSync.Build(project);
...

Upvotes: 1

Related Questions