Need Help With Threading Issue...
-
Hi, I am new to .Net and working with threads for the first time. Please review the code below. Dim t As Thread t = New Thread(AddressOf Me.GetValue) ' GetValue is a procedure t.Start() In GetValue, I want to get some data from the DB and fill in a combobox. However, when the following code in GetValue executes - str = "select ID from ABC" DA = New OleDbDataAdapter(str, ipcon) DS = New DataSet DA.Fill(DS, "rec") - the control is lost. i.e: the next set of statements (to fill the combobox with the dataset values) do not execute. I am not sure why such a thing is happening. Is there some way to handle this threading issue that I am missing? Please help. With Best Regards, Mayur
-
Hi, I am new to .Net and working with threads for the first time. Please review the code below. Dim t As Thread t = New Thread(AddressOf Me.GetValue) ' GetValue is a procedure t.Start() In GetValue, I want to get some data from the DB and fill in a combobox. However, when the following code in GetValue executes - str = "select ID from ABC" DA = New OleDbDataAdapter(str, ipcon) DS = New DataSet DA.Fill(DS, "rec") - the control is lost. i.e: the next set of statements (to fill the combobox with the dataset values) do not execute. I am not sure why such a thing is happening. Is there some way to handle this threading issue that I am missing? Please help. With Best Regards, Mayur
Try Instantiating the Datatable on the main thread before you start the second thread:
Dim dt As New DataTable("dt") Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim thrFillTable As New Threading.Thread(AddressOf FillTable) thrFillTable.Start() End Sub Private Sub FillTable() Dim c1 As New DataColumn("Number") dt.Columns.Add(c1) Dim dr As DataRow For i As Integer = 0 To 10 dr = dt.NewRow dr.Item(0) = i.ToString dt.Rows.Add(dr) 'Next line is just to waste time 'so you will see combo has no data 'till thread Is finished Threading.Thread.Sleep(2000) Next Me.ComboBox1.DataSource = dt Me.ComboBox1.DisplayMember = "Number" End Sub