我目前正在为我的公司做一个小型的自动更新项目。在对多线程进行了一些研究之后,我成功地构建了以下代码:
线程#01 :
Private Sub startUpdate()
If InvokeRequired Then
Invoke(New FTPDelegate(AddressOf startUpdate))
Else
'some code here
End If
End Sub线程#02,由线程#01连接:
Private Sub startProcess()
myThread = New Thread(Sub() startUpdate())
myThread.Start()
myThread.Join()
'another code goes here
Me.close
End Sub当表单加载时访问线程#02:
Private Sub SUpdater_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myThread1 = New Thread(Sub() startProcess())
myThread1.Start()
End Sub有两件事我被困住了:
请帮我改正这个错误。
非常感谢。
发布于 2016-03-14 16:37:50
每次访问UI元素时都需要调用。调用Me.Close()将开始释放表单的所有元素(组件、按钮、标签、文本框等),从而导致与表单本身以及表单中的所有内容的交互。
惟一不需要调用的是您知道在获取或设置时不会修改UI中任何内容的属性,以及字段(也称为变量)。
例如,不需要调用:
Dim x As Integer = 3
Private Sub Thread1()
x += 8
End Sub要解决问题,只需调用表单的闭包即可。这可以简单地使用委托来完成。
Delegate Sub CloseDelegate()
Private Sub Thread1()
If Me.InvokeRequired = True Then 'Always check this property, if invocation is not required there's no meaning doing so.
Me.Invoke(New CloseDelegate(AddressOf Me.Close))
Else
Me.Close() 'If invocation is not required.
End If
End Subhttps://stackoverflow.com/questions/35980288
复制相似问题