1

我有一个关于后台工作人员的简单问题。我从未使用过它,所以我不知道它是如何工作的。我正在使用 VB.NET express 2010。我只想在表单的 backgroundWorker 中进行数据库监控。

以下是我想要实现的几件事。

  1. 使用 select 语句监控 SQLite DB 文件。
  2. 从数据库中提取数据并将它们放入变量中。
  3. 比较具有特定条件的值,如果匹配,则将值传递给另一个表单并调用它。
  4. 继续监控。
  5. 我希望后台工作人员在form.hide()调用表单的方法时执行此操作。

请给出您宝贵的答复,如果我不是正确的方法,请建议另一种方法。

4

1 回答 1

1

隐藏表单不会停止后台工作者——实际上关闭表单不会停止它——表单会等待后台工作者的 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
于 2013-04-23T13:09:27.330 回答