如何从BackGroundWorker获取控制值

时间:2014-03-20 15:52:35

标签: vb.net multithreading backgroundworker

我想从backgroundworker访问控件(例如:textbox,label等)值。我可以轻松地从backgroundworker设置控制值。但现在需要get方法来检索控制值。

这是我的代码......

Private Sub mySQLgetSET_DoWork(sender As Object, e As DoWorkEventArgs) Handles mySQLgetSET.DoWork

    Dim lCount As Integer
    Try

        If conn.State = 0 Then conn.Open()

        If conn.State = 1 Then
            mySQLgetSET.ReportProgress(1, New ControlWithText(lblNetConnection_status, "Connected"))
            lblNetConnection_status.ForeColor = Color.Green
        End If
        If conn.State = 1 Then
            If modem_count > 0 Then
                mySQLgetSET.ReportProgress(1, New ControlWithText(lblNetConnection_status, "Connected"))
                lblNetConnection_status.ForeColor = Color.Green



                mySQLgetSET.ReportProgress(1, New ControlWithText(lblloadque, "Pending request: " & lvRec.Items.Count))

                'Load data from server
                If Not lvRec.Items.Count > 0 Then
                    System.Threading.Thread.Sleep(5000)
                    Me.lvRec.Items.Clear()
                    Dim SqlCmd As String = ""
                    SqlCmd = "select * from  send_query where operator like '%GP%' and status='0'  Order by ID asc"
                    Dim cmd As MySqlCommand = New MySqlCommand(SqlCmd, conn)
                    Dim reader As MySqlDataReader = cmd.ExecuteReader()

                    While reader.Read()
                        'Add Data to Listview from backgroundworker
                        ListViewAddItem(Me.lvRec, _
                                        reader.GetString("id"), _
                                        reader.GetString("load_type"), _
                                        reader.GetString("phone"), _
                                        reader.GetString("balance"), _
                                        reader.GetString("user_id"), _
                                        Format(TimeOfDay, "HH:mm:ss"))


                        Application.DoEvents()
                        mySQLgetSET.ReportProgress(1, New ControlWithText(lbldataloading, "Loading Request: " & lvRec.Items.Count))


                    End While
                    reader.Close()
                    mySQLgetSET.ReportProgress(1, New ControlWithText(lbldataloading, "Total Request: " & lvRec.Items.Count))

                ElseIf lvRec.Items.Count > 0 Then

                    mySQLgetSET.ReportProgress(1, New ControlWithText(lblloadque, "Pending request: " & lvRec.Items.Count))

                    For i As Int32 = 0 To modem_count - 1

                        If txt_coredata_servertxt(i).Text = "" And txt_Update_4(i).Text = "" Then
                            Try
                               If StopThread(i) = False And txt_coredata_servertxt(i).Text = "" And txt_Update_4(i).Text = "" Then

                                    Dim newData As String

                                    '***************************************************************
                                    'Here is my problem. I want to acceaa listView Item 
                                    'lvRec is my listView

                                    newData = lvRec.Items(i).SubItems.Item(1).Text
                                    '***************************************************************

                                End If

                                'lblloadque.Text = "Pending request: " & lvRec.Items.Count
                                mySQLgetSET.ReportProgress(1, New ControlWithText(lblloadque, "Pending request: " & lvRec.Items.Count))
                            Catch ex As Exception
                                MsgBox(ex.Message)
                                txt_coredata_servertxt(i).Text = ""

                                mySQLgetSET.ReportProgress(1, New ControlWithText(lblloadque, "Pending request: " & lvRec.Items.Count))
                            End Try
                        End If
                        Application.DoEvents()
                    Next

                    'Checked to Remove Data
                    For lCount = lvRec.Items.Count - 1 To 0 Step -1
                        'If lvRec.Items(lCount).Checked Then
                        '    lvRec.Items(lCount).Remove()
                        'End If
                        'mySQLgetSET.ReportProgress(1, New ControlWithText(lbldataloading, "Total Request: " & lvRec.Items.Count))
                        Application.DoEvents()
                    Next

                End If
            End If

        End If
        If conn.State = 1 Then conn.Close()
    Catch ex As Exception
        'Me.Text = conn.State.ToString
        MsgBox(ex.Message)
        If conn.State = 0 Then
            mySQLgetSET.ReportProgress(100, New ControlWithText(lblNetConnection_status, "Conecting>>"))

            'lblNetConnection_status.Text = "Conecting>>"
            lblNetConnection_status.ForeColor = Color.Blue
        End If
    End Try
    If conn.State = 1 Then conn.Close()


End Sub

1 个答案:

答案 0 :(得分:0)

通常我们使用backgroundworker RunWorkerCompleted事件处理数据,但你可以看看 虚拟Treeview实现,特别是“#Region Asynchronous Treeview”。它会解决你的问题。这是链接

http://www.codeproject.com/Articles/20552/Virtual-Treeview-Implementation