Main form locks when child opens.

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 6 years and 2 weeks 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
SakJaTeda
Posts: 11
Last visit: Fri Jan 20, 2023 2:53 pm

Main form locks when child opens.

Post by SakJaTeda »

Hello,

I am trying to build a multiform application in PSStudio and the issue I have is that I want to include some of my older tools. I am doing this by having a menustrip with "Tools" in it and the desired tools linked there, calling them via Show-<oldertoolname>_psf, having added the appropriate psf files into the project. All works well, except, I would like to keep the main form interactive when one of these tools are opened, since they are basically standalone. Currently, whenever I open one of the tools, the main form locks until the time the called tool is closed.

Is there a way to avoid this?

Thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Main form locks when child opens.

Post by jvierra »

This is the way PowerShell forms work. PowerShell only allow one modal form to be displayed at a time. The topmost form will be the only one active. PS only allows "ShowDialog" which produces only modal dialogs.

Three are methods using runspaces to launch a modal dialog as a separate PS thread. PowerShell Studio has not built-in support for runspaces so al forms have to be external ps1 files to be called.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Main form locks when child opens.

Post by jvierra »

Here is the quick way to launch a form in a separate runspace.

Code: Select all

$f = get-item Function:\Show-TestForm_psf
$ps = [powershell]::Create()
$ps.AddScript($f.Definition)
$ps.BeginInvoke()
You will have to devise code and a protocol to clean up runspaces that have been ended.
User avatar
SakJaTeda
Posts: 11
Last visit: Fri Jan 20, 2023 2:53 pm

Re: Main form locks when child opens.

Post by SakJaTeda »

Hi jvierra,

This is exactly what I was looking for, but being a newb to all this PS GUI stuff, I didn't know whether a job would do the job here ( :D ) or if something else was the solution.

Anyway, I added a timer that monitors for the child form runspace's IsCompleted and disposes of it when the form is closed.

Thanks a lot for the assist! :)
This topic is 6 years and 2 weeks 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