@@ -1239,6 +1239,71 @@ Describe "Start-ProcessEx" {
12391239 }
12401240 }
12411241
1242+ It " Cancels process -Wait" {
1243+ $stdout = [System.IO.Pipes.AnonymousPipeServerStream ]::new(" In" , " Inheritable" )
1244+ $stdin = [System.IO.Pipes.AnonymousPipeServerStream ]::new(" Out" , " Inheritable" )
1245+ try {
1246+ $si = New-StartupInfo - StandardOutput $stdout.ClientSafePipeHandle - StandardInput $stdin.ClientSafePipeHandle
1247+ $procParams = @ {
1248+ FilePath = ' powershell.exe'
1249+ ArgumentList = ' -Command' , " -"
1250+ StartupInfo = $si
1251+ Wait = $true
1252+ PassThru = $true
1253+ }
1254+
1255+ $projectPath = Split-Path - Path $PSScriptRoot - Parent
1256+ $ps = [PowerShell ]::Create()
1257+ [void ]$ps.AddCommand (" Import-Module" ).AddParameter(" Name" , " $projectPath \output\ProcessEx" ).AddStatement()
1258+ $task = $ps.AddCommand (' Start-ProcessEx' ).AddParameters($procParams ).BeginInvoke()
1259+ try {
1260+ $stdinWriter = [IO.StreamWriter ]::new($stdin )
1261+ $stdinWriter.AutoFlush = $true
1262+ $stdoutReader = [IO.StreamReader ]::new($stdout )
1263+
1264+ $stdinWriter.WriteLine (' $pid' )
1265+ [int ]$childPid1 = $stdoutReader.ReadLine ()
1266+
1267+ # Wait until we know the process has started (read the input) before disposing the client copies
1268+ $stdin.DisposeLocalCopyOfClientHandle ()
1269+ $stdout.DisposeLocalCopyOfClientHandle ()
1270+
1271+ $stdinWriter.WriteLine (' (Start-Process powershell.exe -PassThru).Id' )
1272+ [int ]$childPid2 = $stdoutReader.ReadLine ()
1273+ $stdoutReader.Dispose ()
1274+
1275+ try {
1276+ $spawnedProcess = Get-Process - Id $childPid1 - ErrorAction SilentlyContinue
1277+ $spawnedProcess | Should -Not - Be $null
1278+ $task.IsCompleted | Should - Be $false
1279+
1280+ $stdinWriter.WriteLine (' exit' )
1281+ $stdinWriter.Dispose ()
1282+ $spawnedProcess.WaitForExit (5000 )
1283+ $task.IsCompleted | Should - Be $false
1284+
1285+ Wait-Process - Id $childPid1 - Timeout 5 - ErrorAction SilentlyContinue
1286+ $task.IsCompleted | Should - Be $false
1287+
1288+ $null = $ps.BeginStop ($null , $null )
1289+ $task.AsyncWaitHandle.WaitOne (5000 )
1290+ $task.IsCompleted | Should - Be $true
1291+ }
1292+ finally {
1293+ Stop-Process - Id $childPid1 - Force - ErrorAction SilentlyContinue
1294+ Stop-Process - Id $childPid2 - Force - ErrorAction SilentlyContinue
1295+ }
1296+ }
1297+ finally {
1298+ $ps.EndInvoke ($task )
1299+ }
1300+ }
1301+ finally {
1302+ $stdout.Dispose ()
1303+ $stdin.Dispose ()
1304+ }
1305+ }
1306+
12421307 It " Fails with suspend and -Wait" {
12431308 $err = $null
12441309 Start-ProcessEx pwsh - CreationFlags Suspended - Wait - ErrorAction SilentlyContinue - ErrorVariable err
0 commit comments