可以使用異步編程:
Dim connectionString As String = "server=.\SQLEXPRESS; database=master; Integrated Security=true; Asynchronous Processing=true"
Private Sub btnDisplayCustomersCallback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayCustomersCallBack.Click
Dim sqlConnection As New SqlConnection(connectionString)
Dim sqlCommand As SqlCommand = sqlConnection.CreateCommand()
Dim asyncResult As IAsyncResult
'Example of Asynchronous Callback Model
sqlCommand.CommandText = "SELECT * FROM [customer]"
sqlCommand.CommandType = CommandType.Text
sqlConnection.Open()
btnDisplayCustomersCallBack.Enabled = False
Dim callback As AsyncCallback = New AsyncCallback(AddressOf DisplayCustomers)
asyncResult = sqlCommand.BeginExecuteReader(callback, sqlCommand, CommandBehavior.CloseConnection)
End Sub
Private Sub DisplayCustomers(ByVal result As IAsyncResult)
Dim dr As SqlDataReader
Dim command As SqlCommand
Dim del As DelFillGrid
Try
command = CType(result.AsyncState, SqlCommand)
dr = command.EndExecuteReader(result)
del = New DelFillGrid(AddressOf FillGrid)
Threading.Thread.Sleep(5000)
Me.Invoke(del, dr)
Finally
If (Not dr.IsClosed) Then
dr.Close()
End If
End Try
End Sub
Private Sub FillGrid(ByVal dr As SqlDataReader)
Try
Dim dt As New DataTable()
dt.Load(dr)
Me.DataGridView1.DataSource = dt
Catch ex As Exception
' Because you're guaranteed this procedure
' is running from within the form's thread,
' it can directly interact with members of the form.
Finally
btnDisplayCustomersCallBack.Enabled = True
If dr IsNot Nothing Then
dr.Close()
End If
End Try
End Sub
在此,應用程序將產生asynchrnous方法的ExecuteReader請求,並在結果得到的網格被填滿。直到那時應用程序進行進一步處理。
如果本地需要600ms,則表明這是帶寬問題;但2MB雖然不重要,但遠非「巨大」 - 服務器和客戶端之間的連接是什麼? – 2012-03-12 09:43:13
2mb現在也很嚴重瑣碎 - 不是說它不應該被避免,而是20秒內通過局域網,無線或DSL傳輸30秒也是不現實的。 – TomTom 2012-03-12 09:44:38
請檢查時間已到。我認真地認爲你看錯了一個項目。 CHeck沒有UI綁定,然後檢查UI綁定需要多長時間。我覺得你簡單的在UI中使用那段時間。 – TomTom 2012-03-12 09:45:26