2010-10-19 90 views
1

任何人都可以給我一個如何在Powershell中使用BackgroundWorker的例子嗎?Powershell Backgroundworker

我想寫一些東西,當我的Powershell GUI應用程序中選擇新選項卡時,它將切換到顯示「請稍候」消息的選項卡,然後在BackgroundWorker線程中運行一些檢查,然後進行更新。

這是可能的Powershell?所有谷歌搜索我已經做了點C#或VB.Net。

乾杯,

回答

1

如果後臺線程會使用PowerShell管道來完成工作,那麼我會盡量避免使用BackgroundWorker的。它不會綁定到PowerShell運行空間。嘗試做使用Register-ObjectEvent例如爲:

Register-ObjectEvent $tabItem Loaded -Action { <do bg work here> } 
0

您的異步處理您還可以使用Start-Process

$scriptPath = "c:\script.ps1" 
$allArgs = "/someOrNoArgsHere /moreArgs" 
$backgroundProcess = Start-Process -FilePath $scriptPath -ArgumentList $allArgs -Wait -Passthru -WindowStyle Hidden 
$backgroundProcess.WaitForExit() 
$backgroundProcess.ExitCode 

或者你可以使用一個班輪:

Start-Process -FilePath "c:\script.ps1" -ArgumentList "/someOrNoArgsHere /moreArgs" -Wait -Passthru -WindowStyle Hidden