线程在vb.net中无法正常工作..我使用线程查询数据库

时间:2012-09-10 17:54:41

标签: vb.net multithreading db2

当查询长时间运行并且执行了任何UI操作时,我的Windows应用程序进入“无响应状态”。 所以我去了线程概念。但是在Thread.start()方法中执行数据库操作时。它给出了“windows应用程序已停止工作”等错误。

我的代码:

 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

错误出现在'custDA.Fill(custDS)'

行中

错误 - Windows应用程序已停止工作

任何人都可以帮助我......

1 个答案:

答案 0 :(得分:1)

不要自己管理一个帖子,而是看看背景工作者。

Dim bw As New Backgroundworker
bw.RunWorkerAsync

在backgroundworker的“DoWork”事件处理程序中,实现数据库下载功能。在backgroundworker的'RunWorkerCompleted'事件处理程序中更新您的UI。