'PowerShell: Start-Process with wait option taking long time to return
I have the following script:
$mArgs = @('myProj.vcxproj', '/t:Clean,Build' ,('/p:configuration=DEBUG'+';platform=win32;OutDir=./'))
Start-Process msbuild.exe -ArgumentList $mArgs -RedirectStandardOutput $tempFile -wait
The above successfully builds myProj. However, it takes a really long time to return. When the above line is reached, I see the msbuild windows for about 2 minutes. Then, it closes. After that, it takes another 8 minutes for the process to complete. If I just run the above in a cmd window, it takes about 2 minutes for it to complete. I tried starting the process cmd.exe and passing msbuild as a parameter, but got the same result. I also tried Invoke-Expression and also got the same results.
Does anyone have a clue what can be causing this delay ?
Thanks in advance!
Solution 1:[1]
I am using PowerShell v4.0 Start-Process for running MSBuild on Win2012R2. Usually build time of my solution is 1 min, but on build server it is 16 min. It takes MSBuild 15 min to close all nodes and finally exit (16 min to print "Finished!!!").
PowerShell code:
$process = Start-Process -FilePath $fileName -ArgumentList $arguments
-WorkingDirectory $workingDir -NoNewWindow -PassThru -Wait
Write-Host "Finished!!!"
$exitCode = $process.ExitCode
MSBuild args:
$commandLine = "/nologo /p:Configuration=Release;Platform=x64 /maxcpucount:2"
+ " "+ $dir + "MySolution.sln"
After adding /nodeReuse:false to MSBuild command line the build time is back to normal (1min).
Here is the working code:
function PsStartProcess([string]$fileName, [array]$arguments, [string]$workingDir)
{
if (!$workingDir)
{
$workingDir = [System.IO.Directory]::GetCurrentDirectory()
}
$process = Start-Process -FilePath $fileName -ArgumentList $arguments -WorkingDirectory $workingDir -NoNewWindow -PassThru -Wait
Write-Host "Finished!!!"
$exitCode = $process.ExitCode
$process.Close()
return $exitCode
}
$exeFileName = "c:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe"
$commandLine = "/nologo /p:Configuration=Release;Platform=x64 /maxcpucount:2 /nodeReuse:false" + " "+ $dir + "MySolution.sln"
PsStartProcess $exeFileName $commandLine $binDir
Solution 2:[2]
I had same issue using devenv.exe to compile like this
Start-Process $devenv $args -Wait
That would take forever. I reformatted the code to this
$proc = Start-Process $devenv $args -PassThru
$proc.WaitForExit()
And it is flying. In my case it is compilation of multiple solutions (40+) in the loop, and in perf-mon I don't see many active MSBuild/Devenv processes. There are few of each but all "terminated". And memory is OK. So, good option.
Solution 3:[3]
Why are you using Start-Process to run MSBUILD? I run it directly within PowerShell e.g.:
C:\PS> msbuild myproj.vcxproj /t:clean`,build /p:configuration=DEBUG`;platform=win32`;OutDir=. > $tempfile
Just be sure to escape the characters that PowerShell would normally interpret like , and ;.
Solution 4:[4]
I ran into this problem today. What I found is that msbuild waits for VBCSCompiler.exe (located in the visual studio installation folder) to close. There is a config to this exe with a keepalive setting which defaults to 600 seconds. Setting this to 1 second will bypass this problem.
Solution 5:[5]
Just observed the script and found that the first Line of the script seems to have a missing Single Quote. Please try again with the missing quote and report if that helps?
Solution 6:[6]
On Windows 7 the process returns immediately after completion of the build, but on Windows 8 I have the same problem. If possible, I'll try to test with some other environments as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergei Zinovyev |
| Solution 2 | T.S. |
| Solution 3 | Keith Hill |
| Solution 4 | thomas271828 |
| Solution 5 | SavindraSingh |
| Solution 6 | wensveen |
