使用事件从另一个类的后台工作者更新UI

时间:2015-04-06 16:53:19

标签: vb.net multithreading class backgroundworker

我正在尝试使用事件来更新来自不同类的后台工作程序的文本框。

除了我使用VB.NET之外,它与this SO post中提到的问题相同。我试图通过@ sa_ddam213实现第二个建议的解决方案。

我收到错误:“跨线程操作无效:控制' txtResult'从创建它的线程以外的线程访问。“

这是我的代码:

Public Class DataProcessor
    Public Delegate Sub ProgressUpdate(ByVal value As String)
    Public Event OnProgressUpdate As ProgressUpdate

    Private Sub PushStatusToUser(ByVal status As String)
        RaiseEvent OnProgressUpdate(status)
    End Sub

    Public Sub ProcessAllFiles(ByVal userData As UserData)
        'Do the work
    End Sub
End Class

Public Class MainForm
    Private bw As New BackgroundWorker
    Private dp As New DataProcessor

    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)

        If bw.CancellationPending = True Then
            e.Cancel = True
        Else
            dp.ProcessAllFiles(CType(e.Argument, UserData))
        End If
    End Sub

    Private Sub dp_OnProgressUpdate(ByVal status As String)
        txtResult.Text = status
    End Sub

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bw.WorkerReportsProgress = True
        bw.WorkerSupportsCancellation = True
        AddHandler bw.DoWork, AddressOf bw_DoWork
        AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
        AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted

        AddHandler dp.OnProgressUpdate, AddressOf dp_OnProgressUpdate
    End Sub
End Class

全部谢谢!

2 个答案:

答案 0 :(得分:5)

该事件仍然来自与UI不同的线程。您需要将唤醒委托给UI。我不会检查InvokeRequired,因为我知道它来自工作线程。

Me是表单,Invoke要求代理人处理将数据带到UI线程的工作。在这里,我的代表Sub是一个lambda Sub,而不是使用普通的Sub例程 - 更简单的设计。

Private Sub dp_OnProgressUpdate(ByVal status As String)
   'invoke the UI thread to access the control
   'this is a lambda sub
   Me.Invoke(Sub
               'safe to access the form or controls in here
               txtResult.Text = status
             End Sub)
 End Sub

答案 1 :(得分:1)

也许你可以尝试做这样的事情?

    Private Delegate Sub del_Update(ByVal status As String)

    Private Sub dp_OnProgressUpdate(ByVal status As String)
        If txtResult.InvokeRequired Then
            txtResult.Invoke(New del_Update(AddressOf dp_OnProgressUpdate),  New Object() {status})
        Else
            target_textbox.text = status 
        End If

    End Sub