@@ -491,15 +491,31 @@ function Install-UnitySetupInstance {
491
491
}
492
492
493
493
if ( $downloadSource.Length -gt 0 ) {
494
- for ($i = 0 ; $i -lt $downloadSource.Length ; $i ++ ) {
495
- Write-Verbose " Downloading $ ( $downloadSource [$i ]) to $ ( $downloadDest [$i ]) "
496
- $destDirectory = [io.path ]::GetDirectoryName($downloadDest [$i ])
497
- if (! (Test-Path $destDirectory - PathType Container)) {
498
- New-Item " $destDirectory " - ItemType Directory | Out-Null
494
+ [System.Net.WebClient []]$webClients = @ ()
495
+ try {
496
+ for ($i = 0 ; $i -lt $downloadSource.Length ; $i ++ ) {
497
+ Write-Verbose " Downloading $ ( $downloadSource [$i ]) to $ ( $downloadDest [$i ]) "
498
+ $destDirectory = [io.path ]::GetDirectoryName($downloadDest [$i ])
499
+ if (! (Test-Path $destDirectory - PathType Container)) {
500
+ New-Item " $destDirectory " - ItemType Directory | Out-Null
501
+ }
502
+
503
+ $webClient = New-Object System.Net.WebClient
504
+ $webClient.DownloadFileAsync ($downloadSource [$i ], $downloadDest [$i ])
505
+ $webClients += $webClient
499
506
}
500
507
501
- (New-Object System.Net.WebClient).DownloadFile($downloadSource [$i ], $downloadDest [$i ])
508
+ # Wait for all the downloads to finish
509
+ while ( $webClients.Where ({ $_.IsBusy }, ' First' ).Count -gt 0 ) {}
510
+
511
+ # Clear the list so the finally does no work
512
+ $webClients = @ ()
502
513
}
514
+ finally {
515
+ # If the script is stopped, e.g. Ctrl+C, we want to cancel any downloads
516
+ $webClients | ForEach-Object { $_.CancelAsync () }
517
+ }
518
+
503
519
}
504
520
505
521
for ($i = 0 ; $i -lt $localInstallers.Length ; $i ++ ) {
0 commit comments