国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PowerShell > 正文

PowerShell多線程執行前后臺作業的例子

2020-03-26 19:25:15
字體:
來源:轉載
供稿:網友

下面例子使用Powershell線程運行了兩個后臺任務和一個前臺任務,創建幾個運行時間長點的任務,并且每個任務命令中添加使用Start-Sleep。

 

復制代碼 代碼如下:

$start = Get-Date

$task1 = { Start-Sleep -Seconds 4; Get-Service }
$task2 = { Start-Sleep -Seconds 5; Get-Service }
$task3 = { Start-Sleep -Seconds 3; Get-Service }

# run 2 in separate threads, 1 in the foreground
$thread1 = [PowerShell]::Create()
$job1 = $thread1.AddScript($task1).BeginInvoke()

$thread2 = [PowerShell]::Create()
$job2 = $thread2.AddScript($task2).BeginInvoke()

$result3 = Invoke-Command -ScriptBlock $task3

do { Start-Sleep -Milliseconds 100 } until ($job1.IsCompleted -and $job2.IsCompleted)

$result1 = $thread1.EndInvoke($job1)
$result2 = $thread2.EndInvoke($job2)

$thread1.Runspace.Close()
$thread1.Dispose()

$thread2.Runspace.Close()
$thread2.Dispose()

$end = Get-Date
Write-Host -ForegroundColor Red ($end - $start).TotalSeconds

 

相繼執行這3個任務從Start-Sleep中看至少需要花費12秒。但是這個腳本僅執行了5秒多一點。其結果保存為$result1, $result2和$result3。與后臺作業對比,它在返回大數據用時將差不多。

文章出處:http://www.pstips.net/

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太仆寺旗| 新郑市| 繁昌县| 嵊泗县| 大新县| 交城县| 泽普县| 枣庄市| 安新县| 高台县| 盈江县| 秦皇岛市| 北宁市| 太谷县| 客服| 闽侯县| 镇原县| 谷城县| 闻喜县| 海兴县| 南皮县| 伊宁市| 双流县| 铜梁县| 安岳县| 合江县| 林西县| 城固县| 宜阳县| 清苑县| 罗平县| 边坝县| 霍州市| 靖西县| 宽城| 贵德县| 昌平区| 偃师市| 兴安盟| 曲松县| 连山|