Question BackgroundWorker and GUI update

Budius

Well-known member
Joined
Aug 6, 2010
Messages
137
Location
UK
Programming Experience
3-5
I know it's a recurrent thematic, but I've read everything I could and all seems to be programmed correctly, no exceptions being throw without a reason, no dead locks, no busy sleeps or anything...
It's a fairly big system so I can't post ALL the code, but I'll post the bits that I think it's relevant.

I have 2 mains windows (with shapes, text boxes, labels, listboxes) each of those create it's own instance of my class "SerialConnection" and run the read function continuously on a separate thread, so both looks pretty much like this:

VB.NET:
    Private Sub CommsReader_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles CommsReader.DoWork

        While Not CommsReader.CancellationPending
            If Serial.ReadWTU() Then                ' If read successful
                CommsReader.ReportProgress(1)   ' Reports 1 = good read, update screen
            Else                                             ' If Timeout
                CommsReader.ReportProgress(-1)  ' Report -1 = time out
                Exit While                                 ' Stops the coms reader
            End If
            r.Sleep(1000)
        End While
    End Sub

The "Serial.Read" function reads and break in parts the stream that is been sent on the serial port once a second and stores it's values in a public Analogues(20) as string in a separate module.
The CommsReader.ProgressChanged handler function process those strings into doubles, integers, and booleans and from those values updates the UI colours, visibility, put messages on the list boxes. stuff such as:
VB.NET:
Pressure.Text = Format(WTU.gas_pressure, "0") & " mBar"
If r.BitwiseAnd(WTU.Inputs, 3) Then 
          Inputs.Item(3).FillColor = Color.Green
Else
          Inputs.Item(3).FillColor = Color.Gray
End If

Furthermore, one of those forms upon user input, starts 2 other threads:
- One that is a bit of simple "Select Case, i = i + 1" type of processing that controls the sequence and could really be in a timer that wouldn't make a difference
- and the second thread is a SQL interaction, sending an INSERT every two seconds.

So to sum up we have 5 threads here:
1 GUI;
2 Serial Communication; that writes in separate Public String
1 Simple processing;
1 DB;

Most of time I feel that the whole update time is OK...
but after The simple processing and the DB triggers; you can count almost up to 5 seconds difference from the moment the serial message was sent and the screen actually updates.

I just included now:
VB.NET:
Application.DoEvents()
in the BackgroundWorker and at the end of the screen updater a
VB.NET:
Me.Refresh()

Tomorrow after reading the forum here I'll test it, but I'm really not feeling confident that it will work.

Any tips, suggestions, advices, comments, questions are welcome
 
Back
Top