我有一个关于后台工作人员的简单问题。我从未使用过它,所以我不知道它是如何工作的。
我正在使用 VB.NET Express 2010。
我只想在表单的 backgroundWorker 中进行数据库监控。
以下是我想要实现的几件事。
form.hide()
时执行此操作方法被调用。 请给出您宝贵的答复,如果我不是正确的方法,请建议另一种方法。
最佳答案
隐藏表单不会停止后台 worker ——实际上关闭表单不会停止它——表单会等待后台 worker 的 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
关于.net - 在VB.NET中调用hide方法后我可以使用backgroundWorker吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16155871/