Home:ALL Converter>Using Events to update UI from a backgroundworker with another class

Using Events to update UI from a backgroundworker with another class

Ask Time:2015-04-07T00:53:19         Author:user432209

Json Formatter

I’m trying to use events to update a textbox from a background worker from a different class.

It is the same problem as mentioned in this SO post, except I'm using VB.NET. I'm trying to implement the 2nd suggested solution by @sa_ddam213.

I’m getting an error: “Cross-thread operation not valid: Control 'txtResult' accessed from a thread other than the thread it was created on.”

Here is my code:

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

Thanks all!

Author:user432209,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29475673/using-events-to-update-ui-from-a-backgroundworker-with-another-class
OneFineDay :

The event still comes from a different thread than the UI. You need to delegate the woke back to the UI. I don't check InvokeRequired because I know it is from the worker thread.\n\nMe is the form, Invoke asks for the delegate that will handle the work of bring the data to the UI thread. Here my Delegate Sub is a lambda Sub instead of using a normal Sub routine - simpler design.\n\nPrivate Sub dp_OnProgressUpdate(ByVal status As String)\n 'invoke the UI thread to access the control\n 'this is a lambda sub\n Me.Invoke(Sub\n 'safe to access the form or controls in here\n txtResult.Text = status\n End Sub)\n End Sub\n",
2015-04-06T17:08:59
yy