0

Hi All Masters,

According to advice from master shark 1 and read Tutorial about BackgroundWorker created by Rev.Jim
I'm trying to use BackGroundWorker for querying database an set new labels...here's my code:

Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BgWorker.RunWorkerAsync()
    End Sub

   Private Sub BgWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BgWorker.DoWork

Call MySqlConnect()
            Dim str As String
            str = " Select * from room "
            da = New MySqlDataAdapter(str, conn)
            ds = New DataSet
            da.Fill(ds, "room")
            For i = 0 To ds.Tables("room").Rows.Count - 1
                Dim newLabel As New Label
                newLabel.Text = ds.Tables("room").Rows(i)("room_code")
                newLabel.Name = newLabel.Text
                newLabel.BorderStyle = BorderStyle.FixedSingle
                newLabel.Size = New Drawing.Size(100, 100)
                newLabel.Font = New Font("Tahoma", 15, FontStyle.Regular)
                newLabel.TextAlign = ContentAlignment.TopCenter
                newLabel.BackColor = Color.LightSkyBlue

                Dim ii As Integer = i
                Me.Invoke(Sub()
                          Controls.Add(newLabel)
                          If ds.Tables("room").Rows(ii)("room_status") = "1" Then
                              newLabel.BackColor = Color.LightGreen    
                          ElseIf ds.Tables("room").Rows(ii)("room_status") = "2" Then
                              newLabel.BackColor = Color.Red    
                          End If
                      End Sub)
            Next
            System.Threading.Thread.Sleep(5000)
End Sub

Private Sub BgWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BgWorker.RunWorkerCompleted
        BgWorker.RunWorkerAsync()
End Sub

The codes look goes well but the color of the labels has not change if I enter a value ....if there is something wrong with the codes or something is missing with the codes ?

Please an advice or sample code for me....im newbie for this

Ps: I'm Using Visual Studio 2010 Express

In advance, thanks for any help.

Regards

Edited by Imam_3: edited

2
Contributors
1
Reply
7
Views
2 Years
Discussion Span
Last Post by Reverend Jim
0

A few things to note here. Do not create the control in the BG thread. Make it part of your GUI. Create a Sub to update the existing label with the new value. The Update Sub will handle whether or not to do the update normally (foreground) or through the delegate (background). You might also only do the update if the new value differs from the current value.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.