How to use async methods to load Db Data and keep UI responsive

前端 未结 2 359
梦毁少年i
梦毁少年i 2021-01-15 15:01

I made a somewhat large application that works well, except for its UI (winforms) freezes when using webclient to retrieve data from web (link is um... not the fastest), or

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-15 15:44

    never mind. I've got what I wanted with a BackgroundWorker instead:

    Public Function GetDatatableUIfree() As DataTable
        Dim connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\lanserver\storage\DB.accdb;Persist Security Info=False;")
        Dim command = connection.CreateCommand()
        command.CommandText = "SELECT * FROM bdPROC;"
        connection.Open()
        Dim retTable As New DataTable, bgw As New BackgroundWorker, bgw_complete As Boolean
        AddHandler bgw.DoWork,
            Sub(sender As Object, e As DoWorkEventArgs) retTable.Load(command.ExecuteReader)
        AddHandler bgw.RunWorkerCompleted,
            Sub(sender As Object, e As RunWorkerCompletedEventArgs) bgw_complete = True
        bgw.RunWorkerAsync()
        Do
            Application.DoEvents()
        Loop Until bgw_complete
        connection.Close()
        Return retTable
    End Function
    

    Thank you very much anyway for the help.

提交回复
热议问题