Advertisement
Yevrag35

Background Runspace

Oct 24th, 2020
1,919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $hashUI = [hashtable]::Synchronized(@{})
  2.  
  3. $backgroundRS = [runspacefactory]::CreateRunspace()
  4. $backgroundRS.ApartmentState = "STA"
  5. $backgroundRS.ThreadOptions = "ReuseThread"
  6. $backgroundRS.Open()
  7. $backgroundRS.SessionStateProxy.SetVariable("hashUI", $hashUI)
  8.  
  9. $hashUI.BackgroundJobInfo = [powershell]::Create().AddScript({
  10.     while ($true) {
  11.         # ... do something periodically
  12.  
  13.         Start-Sleep -Seconds 60
  14.     }
  15. })
  16. $hashUI.BackgroundJobInfo.Runspace = $backgroundRS
  17. $hashUI.BackgroundJob = $hashUI.BackgroundJobInfo.BeginInvoke()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement