|
// This is an example of an asynchronous call. SimpleMath has an asynchronous method called BeginAdd. Who can change the SimpleNath class to a form that contains beginxxx and endxxx methods, like asynchronous calls like beginxxx and endxxx in a socket.
using System;
using System.Runtime.Remoting.Messaging;
using System.Threading;
namespace DemoDelegate
{
public delegate int AddDelegate (int n1, int n2);
class Class1
{
static void Main (string [] args)
{
SimpleMath math = new SimpleMath ();
IAsyncResult asyncResult = math.BeginAdd (2,5, new AsyncCallback (AddCallback));
Console.WriteLine ("Press Enter to terminate the program ...");
Console.Read ();
}
// Addition callback function
private static void AddCallback (IAsyncResult ar)
{
AsyncResult async = (AsyncResult) ar;
AddDelegate d = (AddDelegate) async.AsyncDelegate;
int result = d.EndInvoke (ar);
Console.WriteLine ("2 + 5 =" + result);
Ranch
}
}
public class SimpleMath
{
// addition
public int Add (int n1, int n2)
{
Thread.Sleep (2000);
return n1 + n2;
}
// Addition using asynchronous processing
public System.IAsyncResult BeginAdd (int n1, int n2, AsyncCallback callback)
{
AddDelegate d = new AddDelegate (this.Add);
return d.BeginInvoke (n1, n2, callback, null);
}
}
} |
|