Introduction
Synchronous and Asynchronous operation
Synchronous operation– It is a technique to process multiple threads (methods) at about same time. It is like a processing in particular way in which 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 behaviour.
For example, if computer schedules thread A and thread B to process in order within 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 won’t require waiting for 30 seconds. B will get his turn in about 1 second. Yes, the fact behind this is- A will anyway take 30 seconds or may be more for its execution.
Asynchronous Remoting in .Net
In .net environment, the functions which are used to implement synchronous or asynchronous operation are actually delegates. Programming approach for its implementation is 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 asynchrously when 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
Listing 1 –Steps to implement an asynchronous remote method call in client (assuming remote server has been implemented)
Step 1: Implement the client to create 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:
Code
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
Code
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:
Code
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 other waiting thread to continue processing.
oManualResetEvent.Set()
End Sub
Step 5: Now, after creating the remote object (as in Step 1), implement following to invoke remote method asynchronously:
Code
'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 does not affected if method gets delayed or halt for long at server. Here we should be cautious- not to implement very dependent threads or methods to be operated asynchronously. In remoting senario caller has to decide whether it should apply asynchronous behaviour.
Sunday, January 6, 2008
Subscribe to:
Posts (Atom)