我的表单中的方法是在新线程中运行do while循环。
一旦将sentinel值设置为true(由另一个处理Me.FormClosing
的方法设置),for循环将退出
问题是,当表单关闭时,我偶尔会遇到两个例外。
ObjectDisposedException
和ComponentModel.Win32Exception
。
如果没有"吃"我如何正确退出表格?这些例外并忽略它们。
代码:
Dim _exit As Boolean
Public Sub test()
Dim task As Thread = New Thread(
Sub()
Do
checkInvoke(Sub() a.append("a"))
Loop While _exit = False
End Sub)
End Sub
Private Sub checkInvoke(ByVal _call As Action)
If Me.InvokeRequired Then
Me.Invoke(Sub() checkInvoke(_call))
Else
_call.Invoke()
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
_exit = True
End Sub
答案 0 :(得分:1)
错误来自哪里?
这可能有点令人困惑,但实际上它很合乎逻辑......
FormClosing
将_exit
设置为True
_exit
值然后结束循环,一切顺利。ObjectDisposedException
如何防止这种情况?
您可以做的一件事是,在FormClosing
事件中,等待线程结束,然后让系统关闭表单:
Private _isFinished As Boolean = False
Private _exit As Boolean = False
Public Sub test()
Dim task As Thread = New Thread(
Sub()
Do
checkInvoke(Sub() a.append("a"))
Loop While _exit = False
'We inform the UI thread we are done
_isFinished = True
End Sub)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
_exit = True
While Not _isFinished
Application.DoEvent() 'We can't block the UI thread as it will be invoked by our second thread...
End While
End Sub
答案 1 :(得分:0)
我不熟悉VB,但我在c ++中做过类似的事情。主要问题是当窗体关闭时for循环还没有完成。您可以隐藏表单,等待线程完成然后关闭表单。您可以使用标记来标记停止并列线程。