分类:
2008-10-13 17:20:23
Class Form1 : Inherits Form '*** create delegate object to execute method asynchronously Private TargetHandler As GetCustomerListHandler _ = AddressOf DataAccessCode.GetCustomerList '*** create delegate object to service callback from CLR Private CallbackHandler As AsyncCallback _ = AddressOf MyCallbackMethod Sub cmdExecuteTask_Click(sender As Object, e As EventArgs) _ Handles cmdExecuteTask.Click '*** execute method asynchronously with callback TargetHandler.BeginInvoke("CA", CallbackHandler, Nothing) End Sub '*** CLR calls this method when asynchronous method completes '*** note this method runs on worker thread (not the UI thread) Sub MyCallbackMethod(ByVal ar As IAsyncResult) Dim retval As String() retval = TargetHandler.EndInvoke(ar) '*** add code here to trigger updating of user interface End Sub End Class Class DataAccessCode Shared Function GetCustomerList(ByVal State As String) As String() '*** call across network to DBMS or Web services to retrieve data '*** imagine; this method takes several seconds to complete End Function End Class
'*** this method fires at completion of asynchronous call Sub MyCallbackMethod(ByVal ar As IAsyncResult) Try '*** retrieve return value from async call Dim retval As String() retval = TargetHandler.EndInvoke(ar) '*** begin process to update UI UpdateUI("Task complete", retval) Catch ex As Exception Dim msg As String msg = "Error: " & ex.Message UpdateUI(msg, Nothing) End Try End Sub
Sub UpdateUI(StatusMessage As String, Customers As String()) '*** switch control over to primary UI thread Dim handler As New UpdateUIHandler(AddressOf UpdateUI_Impl) Dim args() As Object = {StatusMessage, Customers} '*** call BeginInvoke method of Form object Me.BeginInvoke(handler, args) End Sub Delegate Sub UpdateUIHandler(StatusMessage As String, _ Customers As String()) Sub UpdateUI_Impl(StatusMessage As String, Customers As String()) '*** update user interface controls from primary UI thread Me.sbMain.Panels(0).Text = StatusMessage Me.lstCustomers.DataSource = Customers End Sub
Public Class Form1 : Inherits Form '*** create delegate object to execute method asynchronously Private TargetHandler As GetCustomerListHandler _ = AddressOf DataAccessCode.GetCustomerList '*** create delegate object to service callback from CLR Private CallbackHandler As AsyncCallback _ = AddressOf MyCallbackMethod Sub cmdExecuteTask_Click(sender As Object, e As EventArgs) _ Handles cmdExecuteTask.Click '*** execute method asynchronously with callback UpdateUI("Starting task...", Nothing) TargetHandler.BeginInvoke("CA", CallbackHandler, Nothing) End Sub '*** callback method runs on worker thread and not the UI thread Sub MyCallbackMethod(ByVal ar As IAsyncResult) Try Dim retval As String() retval = TargetHandler.EndInvoke(ar) UpdateUI("Task complete", retval) Catch ex As Exception Dim msg As String msg = "Error: " & ex.Message UpdateUI(msg, Nothing) End Try End Sub '*** can be called from any method on form to update UI Sub UpdateUI(StatusMessage As String, Customers As String()) '*** check to see if thread switch is required If Me.InvokeRequired Then Dim handler As New UpdateUIHandler(AddressOf UpdateUI_Impl) Dim args() As Object = {StatusMessage, Customers} Me.BeginInvoke(handler, args) Else UpdateUI_Impl(StatusMessage, Customers) End If End Sub '*** delegate used to switch control over to primary UI thread Delegate Sub UpdateUIHandler(StatusMessage As String, _ Customers As String()) '*** this method always runs on primary UI thread Sub UpdateUI_Impl(StatusMessage As String, Customers As String()) Me.sbMain.Panels(0).Text = StatusMessage Me.lstCustomers.DataSource = Customers End Sub End Class