Labels

Wednesday, August 19, 2009

Asynchronous Method Execution Using Delegates

Synchronous Delegation:
------------------------
Delegate Function GetCustomerListHandler(State As String) As String()
Dim handler1 As GetCustomerListHandler
handler1 = AddressOf DataAccessCode.GetCustomerList

'*** execute method synchronously
Dim retval As String()
retval = handler1.Invoke("CA")

---------------------------------------
ASynchronous Delegation Without CallBack:
--------------------------------------------

'*** create delegate object and bind to target method
Dim handler1 As GetCustomerListHandler
handler1 = AddressOf DataAccessCode.GetCustomerList

'*** execute method asynchronously and capture IAsyncResult object
Dim ar As System.IAsyncResult
ar = handler1.BeginInvoke("CA", Nothing, Nothing)

If (ar.IsCompleted) Then
  '*** retrieve method return value
  Dim retval As String()
  retval = handler1.EndInvoke(ar)
End If
---------------------------------------
Figure 3 Executing Multiple Methods Asynchronously
------------------------
'*** server-side request in code behind an ASP.NET page

'*** execute multiple methods asynchronously
Dim ar1, ar2 As System.IAsyncResult
ar1 = handler1.BeginInvoke("CA", Nothing, Nothing)
ar2 = handler1.BeginInvoke("WA", Nothing, Nothing)

'*** capture all return values
Dim retval1, retval2 As String()
retval1 = handler1.EndInvoke(ar1)
retval2 = handler1.EndInvoke(ar2)

'*** finish processing request
---------------------------------------
ASynchronous Delegation With CallBack:
---------------------------------------

'*** create delegate object and bind to target method
Dim handler1 As GetCustomerListHandler
handler1 = AddressOf DataAccessCode.GetCustomerList

Private CallbackHandler As AsyncCallback = AddressOf MyCallbackMethod

'*** execute method asynchronously and capture IAsyncResult object
handler1.BeginInvoke("CA", CallbackHandler, Nothing)

End If

 Sub MyCallbackMethod(ByVal ar As IAsyncResult)
    '*** this code fires at completion of each asynchronous method call
    Dim retval As String()
    retval = TargetHandler.EndInvoke(ar)
  End Sub
---------------------------------------

 

Thanks & Regards,

Arun Manglick || Senior Tech Lead

 

 

No comments:

Post a Comment