C# Source Code: Using delegates to perform asynchronous calls on remote objects
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Using delegates to perform asynchronous calls on remote objects
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, April 13, 2004
Hits:
809
Category:
Unspecified
Article:
The .NET framework provides a feature called asynchronous delegates that allows methods to be called asynchronously with minimal additional code. The example below shows how to call a server maths method which takes 5 secs to complete and then perform some work on the client, before waiting for the results back from the server. The code is split into three name spaces (projects): 1. vbUsers.Remoting.Client. This is the client code. 2. vbUsers.Remoting.Server. This is the server code. 3. vbUsers.Remoting.Shared. This is the shared library that you will need to reference from both the other projects. Do this by opening either of the other two projects. Next right click on the project solution in the solution explorer and select "Add" > "Existing Project", then browse to the location of the share assembly and double click on the solution (.sln) file. Finally, right click on either the server or client project (depending on which one you are adding the reference to) in the solution explorer and select "Add Reference". Then click on the "Projects" tab and double click the share assembly project in the top window and then press "OK". Notes: 1. All the client and server projects should be created as console applications with the shared library created as class library. 2. You will need to add a reference to the "System.Runtime.Remoting" assembly. /* ----------SHARED PROJECT CODE---------- --------------------------------------- --------------------------------------- Copyright Andrew Baker (www.vbusers.com) */ using System; using System.Runtime.Remoting.Messaging; //The shared interface required by the client and the server namespace vbUsers.Remoting.Shared { public abstract class IMathRemoteObject: MarshalByRefObject { public abstract void DoComplexMath(int timeDelay); public abstract float GetMathResults(); } } /* ----------SERVER PROJECT CODE---------- --------------------------------------- --------------------------------------- Copyright Andrew Baker (www.vbusers.com) */ using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Messaging; using System.Collections; using System.Threading; //References to Private assemblies using vbUsers.Remoting.Shared; namespace vbUsers.Remoting.Server { class MathRemoteObject: IMathRemoteObject { float zfGetMathResults = -1; public MathRemoteObject() { Console.WriteLine("MathRemoteObject.Constructor called"); } public override float GetMathResults() { //Return the results Console.WriteLine("MathRemoteObject.GetMathResults called"); return zfGetMathResults; } public override void DoComplexMath(int timeDelay) { Console.WriteLine("MathRemoteObject.DoComplexMath called"); //Simulate some complex maths operation //Simulate some complex maths operation Console.WriteLine("MathRemoteObject.DoComplexMath. Delaying {0} secs before continuing",timeDelay/1000); Thread.Sleep(timeDelay); //Store the results zfGetMathResults = (float)(timeDelay * 5); } } //Main class to host the remote objects class ServerStartup { static void Main(string[] args) { Console.WriteLine("ServerStartup.Main called"); //Register the channel HttpChannel chnl = new HttpChannel(1234); ChannelServices.RegisterChannel(chnl); //Register the maths object RemotingConfiguration.RegisterWellKnownServiceType(typeof(MathRemoteObject),"MathRemoteObject.soap", WellKnownObjectMode.Singleton); // the server will keep running until keypress. Console.ReadLine(); } } } /* ----------CLIENT PROJECT CODE---------- --------------------------------------- --------------------------------------- Copyright Andrew Baker (www.vbusers.com) */ using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Proxies; using System.Threading; //References to Private assemblies using vbUsers.Remoting.Shared; namespace Client { class Client { delegate void delDoComplexMath(int timeDelay); static void Main(string[] args) { DateTime start = System.DateTime.Now; HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); IMathRemoteObject oMathEngine = (IMathRemoteObject) Activator.GetObject(typeof(IMathRemoteObject),"http://localhost:1234/MathRemoteObject.soap"); Console.WriteLine("Client.Main. Reference to remote oMathEngineect acquired"); //Create the delegate to do the work Console.WriteLine("{0}. Client.Main. Calling DoComplexMath Delegate",DateTime.Now.ToString()); delDoComplexMath delServerMaths = new delDoComplexMath(oMathEngine.DoComplexMath); //Start the delegate working. Note, pass in two additional null parameters after our required parameter IAsyncResult svAsyncres = delServerMaths.BeginInvoke(5000,null,null); //Simulate doing other stuff here Console.WriteLine("{0}. Client.Main. Performing local calculations",DateTime.Now.ToString()); Thread.Sleep(4000); //Call the EndInvoke Console.WriteLine("{0}. Client.Main. Waiting for server delegate to return",DateTime.Now.ToString()); delServerMaths.EndInvoke(svAsyncres); Console.WriteLine("{0}. Client.Main. Server delegate has returned value {1}",DateTime.Now.ToString(),oMathEngine.GetMathResults()); Console.ReadLine(); } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet