在主窗体关闭时关闭多个父窗体

时间:2018-12-05 16:22:16

标签: .net vb.net forms exit mdi

我有一个vb.net应用程序,但我没有使用该应用程序框架:

enter image description here

此应用程序具有以下主要功能:

Imports System.Threading

Module Main

    Private _sharedThing As SharedThing = New SharedThing()
    Private _appRunner As AppRunner = New AppRunner()
    Private _firstForm As Form
    Private _secondForm As SecondParent
    Public Event CloseApplication()

    Sub StartFirstParent()

        Dim firstForm = New Form1(_sharedThing, _appRunner, _secondForm)
        Application.Run(firstForm)

    End Sub


    Sub Main()

        Dim mainForm As New Form1(_sharedThing, _appRunner, _secondForm)
        Application.Run(mainForm)
        Application.Exit()
    End Sub


End Module

如您所见,我在一个窗体上调用Application.Run,​​然后使用按钮创建另一个窗体,SecondParent窗体。因此,我有两种父母形式。这是Form1的代码:

Imports System.Threading

Public Class Form1

    Private _sharedThing As SharedThing
    Private _appRunner As AppRunner
    Public Event CloseApplication()
    Private _otherParentForm As SecondParent

    Public Sub New(aSharedThing As SharedThing, ByRef appRunner As AppRunner, otherParentForm As SecondParent)

        _otherParentForm = otherParentForm
        _sharedThing = aSharedThing
        _appRunner = appRunner
        _otherParentForm = otherParentForm

        Me.IsMdiContainer = True
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        _sharedThing.SetString("First Parent: Form1")

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        _sharedThing.ShowString()

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Me.Close()

    End Sub


    Private Sub CreateSecondParent(sender As Object, e As EventArgs) Handles Button4.Click

        Dim SecondThread As New Thread(New ThreadStart(AddressOf StartSecondParent))
        SecondThread.Start()

    End Sub


    Sub StartSecondParent()

        Dim secondForm As Form = New SecondParent(_sharedThing, Me)
        Application.Run(secondForm)

    End Sub


End Class

这是SecondParent的构造函数:

Public Class SecondParent


    Private _sharedThing As SharedThing

    Private WithEvents _myParent As Form1



    Public Sub New(ByRef aSharedThing As SharedThing, ByRef myParentForm As Form1)

        _myParent = myParentForm
        _sharedThing = aSharedThing

        Me.IsMdiContainer = True

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

我无法找出退出Form1表单时关闭SecondParent表单的方法。

如您所见,主要是调用Application.Exit()。我想避免这种情况或了解其后果。请理解,几乎所有的代码布局方式都是我必须忍受的一个约束,因此,完全改变结构的任何建议都无济于事。如果不清楚,如果我不使用Application.Exit(),则在关闭主窗体时不会关闭创建的新窗体。

由于出现错误,我无法使用事件处理程序。假设我将其放在SecondParent的代码中:

Public Sub KillSwitch_Sensor() Handles _myParent.CloseApplication
    Me.Close()
End Sub

我收到一条错误消息:

 : 'Cross-thread operation not valid: Control 'SecondParent' accessed
 from a thread other than the thread it was created on.'

1 个答案:

答案 0 :(得分:1)

您的问题是变量范围以及其他问题(此处不进行代码审查)。快速而肮脏的修复程序,在Form1上为Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing添加一个方法,然后在该表单关闭时做您需要做的...

例如:循环浏览打开的表单,然后关闭所需的表单。我是否建议这样做(肯定不会),但这是一种选择,而无需重构您拥有的大多数代码。

您可能会遇到的一个问题是Cross-thread operation异常,因为您可能试图从另一个线程关闭表单。如果发生这种情况,则需要调用该控件,它应该可以工作。