CodeSnip: Implementing Asynchronous Remoting in .NET
 
Published: 26 Dec 2007
Abstract
In this article Abhishek briefly describes the methods to implement Asynchronous Remoting in Visual Basic 2005.
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 21533/ 23

Introduction

We can implement remoting in two different ways of operation. They are Synchronous and Asynchronous operation.

Synchronous and Asynchronous operation

Synchronous operation

It is a technique to process multiple threads (methods) at about the same time. It is processing in a particular way in which the execution start-up of the next process depends upon the execution completion of the previous process in the scheduled process list.

Asynchronous operation

In this technique, execution of a process or thread remains independent from other processes or threads which are set for asynchronous behavior.

For example, what if the computer schedules thread A and thread B to process in order within a single CPU environment. Say thread A has a very complex logic and takes 30 seconds to finish. In the synchronous operation, thread B has to wait for 30 seconds to get its turn. But if thread A has been implemented to run asynchronously then thread B will not require waiting for 30 seconds. B will get his turn in about 1 second. Yes, the fact behind this is that A will anyway take 30 seconds or may be more for its execution.

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()
Conclusion

Implementing asynchronous method call in .NET remoting can give high performance to the system since other processes do not get affected if the method gets delayed or halted for long at the server. Here we should be cautious to not implement very dependent threads or methods to be operated asynchronously. In the remoting scenario the caller has to decide whether it should apply asynchronous behavior.

By Abhishek Kumar Singh



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 12:58:44 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search