Labels

Wednesday, June 20, 2007

.NET Delegates: Making Asynchronous Method Calls in the .NET Environment

http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/

http://msdn.microsoft.com/library/default.asp?url=/msdnmag/issues/01/08/async/toc.asp

 

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

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

No comments:

Post a Comment