mrplume
mrplume

Reputation: 183

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

Upvotes: 0

Views: 1143

Answers (2)

mrplume
mrplume

Reputation: 183

Thanks you very much, I have created this function :

do {
    RecordToLog -Message "Waiting..."
    start-sleep -m 100
} until ($CommonHashTable.MainForm.IsHandleCreated)
startsecondrunspace

It's working.

Upvotes: 0

BenH
BenH

Reputation: 10034

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
}

Upvotes: 1

Related Questions