跨线程操作无效:Control' DataGridView1'

时间:2015-06-18 12:15:53

标签: vb.net multithreading backgroundworker

我有一个长时间运行的进程,冻结了GUI,我已经转移到BackgroudWorker线程。

问题是,我在最后一行代码中收到以下错误。

DataGridView1.DataSource = ds.Tables(3)
  

跨线程操作无效:控制' DataGridView1'访问   来自其创建的线程以外的线程。

以下是更多上下文代码:

Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork

        Dim numToDo As Integer = CInt(e.Argument)

        Dim count As Integer = ListBox1.Items.Count
        For n As Integer = 0 To ListBox1.Items.Count - 1
            Dim l_text As String = CStr(ListBox1.Items(n))

            Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://www.xxx.co.uk/yyy/notice/data.feed?text=" & l_text & "&results-page-size=100&start-publish-date=2015-06-01")

            Dim response As System.Net.HttpWebResponse = request.GetResponse()

            Select Case response.StatusCode

                Case System.Net.HttpStatusCode.OK

                    Dim stream As System.IO.Stream = response.GetResponseStream()
                    Dim reader As New System.IO.StreamReader(stream)

                    Dim document As New System.Xml.XmlDocument()

                    document.LoadXml(contents)

                    Dim ds As DataSet
                    ds = New DataSet()
                    ds.ReadXml(New XmlNodeReader(document))

                    If ds.Tables.Count <= 4 Then

                        'Do Nothing

                    Else

                        DataGridView1.DataSource = ds.Tables(3)

据我所知,我不允许将后台工作程序任务与其他任务交叉。但我很困惑,因为Datagridview是在DoWork线程中定义的。所以我不相信我想跨越线程?

非常感谢任何帮助。

0 个答案:

没有答案