隐藏表单不会停止后台工作者——实际上关闭表单不会停止它——表单会等待后台工作者的 isBusy 属性报告 false 后再继续。
更新以回应新评论
您可能最好使用计时器并将其他工作卸载到新线程,请参见下面的示例。如果操作尚未完成,If _worker is nothing
则将停止重新启动操作。请务必_worker = nothing
在流程结束时进行设置,以使其正常工作。
此外,我刚刚快速输入了此内容,它可能无法开箱即用,但应该为您提供一个起点。
Imports System.Threading
Public Class Form1
Dim _worker As Thread
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = 10000
'interval in milliseconds so 1000ms = 1 second / above 10000 = 10 seconds
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
StartWorkerThread()
End Sub
Private Sub StartWorkerThread()
If _worker Is Nothing Then
_worker = New Thread(AddressOf myWorker)
_worker.Start()
End If
End Sub
Private Sub myWorker()
'do your work here...use an event or a delate to fire another sub/function on the main thread if required
'when finished
_worker = nothing
'Important! This will allow the operation to be started again on the timer tick
End Sub
End Class