Skip to content

Commit 2102fb1

Browse files
authored
Merge pull request #133 from jwittner/dev/asyncDownloads
Download all installers in parallel.
2 parents 3be147e + b9beea9 commit 2102fb1

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,31 @@ function Install-UnitySetupInstance {
491491
}
492492

493493
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
499506
}
500507

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 = @()
502513
}
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+
503519
}
504520

505521
for ($i = 0; $i -lt $localInstallers.Length; $i++) {

0 commit comments

Comments
 (0)