|
■No81440 (リツコ さん) に返信
> 時間がかかる為、非同期でdoSomething関数を実行したいと思います。
こんな感じで如何でしょう。
# サンプルコード
$init = {
function doSomething1([int]$time) {
[System.Threading.Thread]::Sleep($time)
Write-Host "--> finish 1 at $(Get-Date)"
}
function doSomething2([int]$time) {
[System.Threading.Thread]::Sleep($time)
Write-Host "--> finish 2 at $(Get-Date)"
}
}
"--> begin at $(Get-Date)"
$job1 = Start-Job -InitializationScript $init -ScriptBlock { doSomething1 15000 }
$job2 = Start-Job -InitializationScript $init -ScriptBlock { doSomething2 45000 }
"--> waiting at $(Get-Date)"
Wait-Job $job1, $job2
"--> waited at $(Get-Date)"
Receive-Job $job1, $job2
"--> end at $(Get-Date)"
Get-Job | Remove-Job
<# 実行結果
--> begin at 09/22/2016 21:54:00
--> waiting at 09/22/2016 21:54:00
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
25 Job25 BackgroundJob Completed True localhost doSomething1 15000
27 Job27 BackgroundJob Completed True localhost doSomething2 45000
--> waited at 09/22/2016 21:54:46
--> finish 1 at 09/22/2016 21:54:16
--> finish 2 at 09/22/2016 21:54:46
--> end at 09/22/2016 21:54:46
#>
|