More threading issues: updating control

dreamdelerium

Active member
Joined
Mar 7, 2008
Messages
36
Programming Experience
1-3
so, i got my threading working, using a background worker. the problem im having now is updating a text label on a form. i have a background worker running to test some state, so the background worker is set to run as long as the program is running. every time it fires, it call a function. this function will test a comport for activitity. if there is activity, i want to change a label's text. its not working, though. heres what i got:

VB.NET:
Public Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim j As Integer = 0
        Dim i As Integer = 0
        Debug.Print("bg worker begin")
        While i = j
            If BackgroundWorker1.CancellationPending Then
                i = 2
                e.Cancel = True
                BackgroundWorker1.Dispose()
                Debug.Print("bg worker middle")
                Exit While
            Else
                CheckForNew()
            End If
        End While
        Debug.Print("bg worker end")
    End Sub

and heres the function it calls:

VB.NET:
Public Sub CheckForNew()
        Debug.Print("checkfornew begin")
'...stuff not related


        Dim con As OleDbConnection
        Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurDir() & "\MyDb.mdb"
        con = New OleDbConnection(constring)
        Dim ds As New DataSet
        Dim da As OleDbDataAdapter
        Dim dt As DataTable
        Dim dr As DataRow
        da = New OleDbDataAdapter("SELECT * FROM tblInboxMsg where MsgStatus='New'", con)
        da.Fill(ds)
        counter = 0

        For Each dt In ds.Tables
            For Each dr In dt.Rows
                counter = counter + 1
                      Next
        Next
        Debug.Print("chekcfornew end")
form1.label10.textt = counter 'nothing happens here
    End Sub

ive tried using Progress changed event, as well. although the event fires, it still wont change the label.

any suggestions?

thanks
jason
 
Hello Again.

I hope that the textt Property is just a typo. ;)
It might be just looking like it doesn't Update, run a Me.Refresh() Or Label10.Refresh().
It can be that GUI is not updating while processes are running. You could also try an Application.DoEvents() in between.

Also there might be another issue when you're trying to access a control from another thread. You should really use the ProgressChanged event (and call CheckForNew from there), instead of directly calling the CheckForNew(). The event comes back to the old thread from where the BGWorker was started, but it can happen that if you call the Sub directly, that it will in the new Thread, without any relation to the old one.

Bobby
 
Back
Top