检查表单是否打开 Winform / Powershell

Check if form is Opened Winform / Powershell

我正在搜索如何检查是否使用 Powershell 打开了 winform 表单,例如 this response for VB.net。我正在使用两个运行空间,我需要在打开表单时启动第二个运行空间。

我的第一个运行空间是 GUI。 UI创建完成后,我打开了

$CommonHashTable.MainForm.ShowDialog()

然后,我尝试测试是否从 PowerShell 主线程打开此表单(来自 VB.net 的片段):

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

测试表单是否打开的更好方法可能是

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

Application.OpenForms() 将是应用程序 Class 而不是表单 class 上的方法。我不确定是否有 Application class 的实例甚至能够使用该方法。如果有的话,我想它应该看起来像这样:

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

非常感谢,我创建了这个函数:

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

有效。