Search code examples
winformspowershellrunspace

Check if form is Opened Winform / Powershell


I'm searching how I could check if a winform form is opened with Powershell, like this response for VB.net. I'm working with two runspaces, and I need to start the second, when my form is opened.

My first runspace is for the GUI. When the UI creation is completed, I opened it

$CommonHashTable.MainForm.ShowDialog()

And then, I'm trying to test if this form is opened (snipet from VB.net) from PowerShell main thread:

If Application.OpenForms().OfType(Of $CommonHashTable.MainForm).Any Then
 ... startsecondrunspace

Solution

  • A better way to test if the form is open might be to

    if ($CommonHashTable.MainForm.IsHandleCreated) {
        startsecondrunspace
    }
    

    Application.OpenForms() would be a method on an Application Class rather than the Form class. I am unsure if there is an instance of the Application class to even be able to use that method. If there was, I would imagine it should look something like this:

    If ($ApplicationObject.OpenForms().OfType(Of $CommonHashTable.MainForm).Any) {
        startsecondrunspace
    }