In .NET environment, the functions which are used to
implement synchronous or asynchronous operation are actually delegates. The programming
approach for its implementation is the same as in single application domain. In
addition, since it is remoting, we need remoting configuration and remote
object implementations. We often need to implement remote method call from
client to be asynchronously when the server takes more time to process the
request and return back to the caller.
Assuming we have remote server and client applications
having single call activation method, we only need to modify the client code to
achieve asynchronous operation for remote method calls.
Implementing Asynchronous Remote method call in the
Remote client application
Step 1: Implement the client to
create a remote object which can connect to the server well-known object.
Step 2: Declare a class level
delegate function for the client which matches the remote method going to be
Public Delegate Function SendAndGetDelegate(ByVal KeyIDAs Integer, _
ByVal DataString As String) As String
Step 3: Declare a class level shared
object of ManualResetEvent by setting False for initial state signal.
Public Shared oManualResetEvent As System.Threading.ManualResetEvent = _
Step 4: Create a Callback shared
procedure in the client as given below:
Shared Sub ClientCallBack(ByVal oIAsyncResult As IAsyncResult)
Dim oSendAndGetDelegate As SendAndGetDelegate = CType(oIAsyncResult, _
Dim ReplyFromRemoteServer As String = _
'Set the manual reset event to allow the waiting thread to continue processing.
Step 5: Now, after creating the
remote object (as in Step 1), implement the following code to invoke remote
'Create a Delegate object for the client and initialize it with the remote
'method call using created remote object.
Dim oSendAndGetDelegate As New SendAndGetDelegate(AddressOf _
' Create the AsyncCallback object initialized with the delegate method
Dim oAsynchCallback As New System.AsyncCallback(AddressOf _
Dim oIAsyncResult As System.IAsyncResult = oSendAndGetDelegate.BeginInvoke _(1000, _
"Data Sring to send to Server", AddressOf ClientService.ClientCallBack, 0)
'Block recalling of this thread until it finishes