1

当查询长时间运行并且执行任何 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 应用程序已停止工作

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

4

1 回答 1

1

与其自己管理线程,不如看看 backgroundworker。

Dim bw As New Backgroundworker
bw.RunWorkerAsync

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

于 2012-09-10T18:14:56.563 回答