2010-10-19 6 views
1

Quelqu'un peut-il me donner un exemple d'utilisation de BackgroundWorker dans Powershell? Je veux écrire quelque chose alors quand un nouvel onglet est sélectionné dans mon application Powershell GUI, il passera à l'onglet avec un message "please wait" affiché, puis exécuter des contrôles dans un thread BackgroundWorker, puis mettre à jour.Powershell Backgroundworker

Est-ce possible dans Powershell? Tout le googling que j'ai fait pointe vers C# ou VB.Net.

Cheers,

Ben

Répondre

1

Si fil d'arrière-plan va utiliser un pipeline PowerShell pour faire son travail, alors j'éviter d'utiliser BackgroundWorker. Il ne serait pas lié à un espace de fonctionnement PowerShell. Essayez de faire votre traitement async utilisant Register-ObjectEvent .: par exemple

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

Vous pouvez également utiliser Start-Process:

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

Ou vous pouvez utiliser une seule ligne:

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