Threading not working properly in vb.net.. i have used threading for quering database
My windows application went to "not responding state" when query is running for long time and any UI operation was performed. So i went for Threading concept. But when performing Database operation inside the Thread.start() method. It gives error like "windows application has stopped working"
Dim Newthread As New Thread(AddressOf test) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load CONNECT_STRING = "Provider=IBMDADB2.DB2COPY1;Password=pwd;Persist Security Info=True;User ID="";Data Source="";dbalias="";" End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click strSQL = "query" Newthread.Start(strSQL) End Sub Function test(ByVal strSQL As String) connection = New OleDbConnection(CON_STRING) connection.Open() Dim selectCMD As OleDbCommand = New OleDbCommand(strSQL, connection) Dim custDA As OleDbDataAdapter = New OleDbDataAdapter custDA.SelectCommand = selectCMD selectCMD.CommandTimeout = 0 Dim custDS As DataSet = New DataSet() custDA.Fill(custDS) Dim chk As New DataGridViewCheckBoxColumn() DataGridView1.Columns.Add(chk) chk.HeaderText = "Select Row" chk.Name = "Select row" DataGridView1.DataSource = custDS.Tables(0) DataGridView1.Refresh() End Function
The error is occuring in the line 'custDA.Fill(custDS)'
Error - windows application has stopped working
Can anyone pls help me...
Instead of managing a thread yourself, look at backgroundworker.
Dim bw As New Backgroundworker bw.RunWorkerAsync
In the 'DoWork' Event handler of the backgroundworker, implement your database download functionality. In the 'RunWorkerCompleted' event handler of the backgroundworker update your UI.