Show_GUIForm from ps1 script without waiting for the GUIForm to close...

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
MikePro85
Posts: 3
Last visit: Fri May 26, 2023 10:43 am

Show_GUIForm from ps1 script without waiting for the GUIForm to close...

Post by MikePro85 »

Hello

I have a ps1 script running (no GUI) but need to open a GUI window from it. The ps1 script however has to continue running and not wait until the GUI form window is closed.

Usually I use the Show-Form_psf command, but if I use that, the powershell script in the background stops/waits until the window has been closed.

Mike
User avatar
rmckay9969
Posts: 63
Last visit: Fri Feb 01, 2019 6:51 pm

Re: Show_GUIForm from ps1 script without waiting for the GUIForm to close...

Post by rmckay9969 »

You could launch your window using a runspace.
  1. [int]$Counter = 1
  2. if($counter -lt 5000){
  3.     $counter++
  4.     Start-Sleep -Seconds 3    
  5. }
  6.  
  7. $RunSpace = [RunSpaceFactory]::CreateRunSpace() #Create new runspace.
  8. $RunSpace.ApartmentState = 'STA'
  9. $RunSpace.ThreadOptions = 'UseNewThread'
  10. $RunSpace.Open()
  11. $Instance = [PowerShell]::Create() #create new powershell instance in runspace.
  12. $Instance.Runspace = $RunSpace
  13. $Instance.Runspace.SessionStateProxy.SetVariable("Counter", $Counter) #pass $Counter variable into runspace
  14. [void]$Instance.AddScript({
  15.     if($counter = 2500){                
  16.         #displays first messagebox while script continues counting in background.
  17.         [System.Windows.Forms.MessageBox]::Show("Count = " + $counter + " Almost Done!")
  18.     }
  19.     $Instance.EndInvoke()            
  20.     $Instance.Dispose()
  21.     $RunSpace.Close()            
  22. }).BeginInvoke()
  23.  
  24. if($counter = 5000){
  25.     Start-Sleep -Seconds 3
  26.     [System.Windows.Forms.MessageBox]::Show("Count = " + $counter + " Job Complete!")
  27. }
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked