Powershell后台工作程序

Powershell后台工作程序,powershell,powershell-2.0,Powershell,Powershell 2.0,有人能给我举一个如何在Powershell中使用BackgroundWorker的例子吗 我想写一些东西,以便在我的Powershell GUI应用程序中选择新选项卡时,它将切换到显示“请稍候”消息的选项卡,然后在BackgroundWorker线程中运行一些检查,然后更新 这在Powershell中可能吗?我所做的所有谷歌搜索都指向c#或VB.Net 干杯 Ben如果后台线程将使用PowerShell管道来完成其工作,那么我将避免使用BackgroundWorker。它不会绑定到PowerSh

有人能给我举一个如何在Powershell中使用BackgroundWorker的例子吗

我想写一些东西,以便在我的Powershell GUI应用程序中选择新选项卡时,它将切换到显示“请稍候”消息的选项卡,然后在BackgroundWorker线程中运行一些检查,然后更新

这在Powershell中可能吗?我所做的所有谷歌搜索都指向c#或VB.Net

干杯


Ben

如果后台线程将使用PowerShell管道来完成其工作,那么我将避免使用BackgroundWorker。它不会绑定到PowerShell运行空间。尝试使用
Register ObjectEvent
执行异步处理,例如:

Register-ObjectEvent $tabItem Loaded -Action { <do bg work here> }
Register ObjectEvent$tabItem已加载-操作{}

如果后台线程将使用PowerShell管道来完成其工作,那么我将避免使用BackgroundWorker。它不会绑定到PowerShell运行空间。尝试使用
Register ObjectEvent
执行异步处理,例如:

Register-ObjectEvent $tabItem Loaded -Action { <do bg work here> }
Register ObjectEvent$tabItem已加载-操作{}

您还可以使用
启动流程

$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

您还可以使用
启动流程

$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