可以使用异步编程:
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