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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.