using System;
using System.Net;
using System.Collections;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
class MyClient
{
private delegate int MyDelegate(int a,int b,int time,string ip);
private static MyDelegate md;
[STAThread]
static void Main(string[] args)
{
DateTime dt=DateTime.Now;
RemotingConfiguration.RegisterWellKnownClientType(typeof(RemoteObject.MyObject), "tcp://localhost:9999/RemoteObject.MyObject");
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props=new Hashtable();
props["port"]=0;
TcpChannel channel = new TcpChannel(props,clientProvider,serverProvider);
ChannelServices.RegisterChannel(channel);
RemoteObject.MyObject app=new RemoteObject.MyObject();
app.MyEvent+=new RemoteObject.MyObject.MyEventHandler(MyEvent);
md=new MyDelegate(app.ALongTimeMethod);
AsyncCallback ac=new AsyncCallback(MyClient.CallBack);
IPHostEntry ipHE=Dns.GetHostByName(Dns.GetHostName());
IAsyncResult Iar=md.BeginInvoke(1,2,300,ipHE.AddressList[0].ToString(),ac,null);
Method();
Console.WriteLine("用了"+((TimeSpan)(DateTime.Now-dt)).TotalSeconds+"秒");
ChannelServices.UnregisterChannel(channel);
Console.ReadLine();
}
public static void CallBack(IAsyncResult Iar)
{
if(Iar.IsCompleted)
{
Console.WriteLine("结果是"+md.EndInvoke(Iar));
}
}
public static void MyEvent(object sender,RemoteObject.MyEventArgs e)
{
Console.WriteLine("来自"+e.IP+"的异步方法完成了"+e.Rate*10+"%");
}
public static void Method()
{
Console.WriteLine("主线程方法开始");
System.Threading.Thread.Sleep(5000);
Console.WriteLine("主线程方法结束");
}
}