CodeSnip: Implementing Asynchronous Remoting in .NET
page 3 of 4
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 22824/ 41

Asynchronous Remoting

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 called:

Listing 1

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.

Listing 2

Public Shared oManualResetEvent As System.Threading.ManualResetEvent = _
    New System.Threading.ManualResetEvent(False)

Step 4: Create a Callback shared procedure in the client as given below:

Listing 3

Shared Sub ClientCallBack(ByVal oIAsyncResult As IAsyncResult)
  Dim oSendAndGetDelegate As SendAndGetDelegate = CType(oIAsyncResult, _
    System.Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate
  Dim ReplyFromRemoteServer As String = _
    oSendAndGetDelegate.EndInvoke(oIAsyncResult)
  'Set the manual reset event to allow the waiting thread to continue processing.
  oManualResetEvent.Set()
End Sub

Step 5: Now, after creating the remote object (as in Step 1), implement the following code to invoke remote method asynchronously.

Listing 4

'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 _
m_oRemote.RecieveAndReply)
' Create the AsyncCallback object initialized with the delegate method
Dim oAsynchCallback As New System.AsyncCallback(AddressOf _
ClientService.ClientCallBack)
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 
oManualResetEvent.WaitOne()

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-11-26 10:29:55 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search