remoting教学四:remoting的事件
来源:互联网 发布:sql server 查询器 编辑:程序博客网 时间:2024/05/17 23:56
remoting教学四:remoting的事件
现在,为了更好的表现事件,我们将客户端和服务器都用winform来表现。
在如下的例子中,将会产生一个服务器端事件和一个客户端事件。
RemoteObject
using System.Windows.Forms;
using System.Collections;
namespace RemoteObject
{
public class MyObject : MarshalByRefObject
{
private int i = 0;
public ArrayList totalPersons = new ArrayList();
public MyObject()
{
MessageBox.Show(Application.ProductName + " MyObject is created!");
}
public int Add(int a, int b)
{
return a + b;
}
public int Count()
{
return ++i;
}
public Person getOnePerson()
{
MessageBox.Show(Application.ProductName + " create a person , and return!");
Person p = new Person();
totalPersons.Add(p);
return p;
}
public void SaveOnePerson(Person p)
{
MessageBox.Show(Application.ProductName + " " + p.Name + " saved a person!");
}
public void DoChange(Person p)
{
lock (this)
{
MessageBox.Show(Application.ProductName + " do! person name changed to " + p.Name);
}
}
}
[Serializable]
public class Person
{
private string name="ddddddfault";
public delegate void NameOnChangedHandle(Person p);
public NameOnChangedHandle NameOnChanged;
public Person()
{
MessageBox.Show(Application.ProductName + " create a person !");
}
public string Name
{
get { return name; }
set { name = value; NameOnChanged(this); }
}
}
}
server:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Collections;
using System.Runtime.Serialization.Formatters;
using RemoteObject;
namespace WinRemoteServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
// Creating the IDictionary to set the port on the channel instance.
IDictionary props = new Hashtable();
props["port"] = 9999;
// Pass the properties for the port setting and the server provider in the server chain argument. (Client remains null here.)
TcpChannel chan = new TcpChannel(props, null, provider);
//TcpServerChannel channel = new TcpServerChannel(props,null,provider);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject.MyObject),
"RemoteObject", WellKnownObjectMode.Singleton);
}
MyObject ob = null;
private void button1_Click(object sender, EventArgs e)
{
ob = (MyObject)Activator.GetObject(typeof(MyObject),"tcp://localhost:9999/RemoteObject");
}
}
}
client:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteObject;
namespace WinRemoteClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
RemoteObject.MyObject remoteobj = null;
RemoteObject.Person r = null;
private void Form1_Load(object sender, EventArgs e)
{
ChannelServices.RegisterChannel(new TcpClientChannel());
remoteobj = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject),
"tcp://localhost:9999/RemoteObject");
}
//调用Add
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(remoteobj.Add(1, 2).ToString());
}
//调用Count
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(remoteobj.Count().ToString());
}
//Create a server person, and saved!
private void button3_Click(object sender, EventArgs e)
{
r = remoteobj.getOnePerson();
r.NameOnChanged = remoteobj.DoChange;
remoteobj.SaveOnePerson(r);
}
void r_NameOnChanged(Person p)
{
MessageBox.Show(Application.ProductName + " client do! person name changed to " + p.Name );
}
//Create a client person, and saved!
private void button4_Click(object sender, EventArgs e)
{
r = new RemoteObject.Person();
remoteobj.SaveOnePerson(r);
r.NameOnChanged = r_NameOnChanged;
}
//改变NAME值
private void button5_Click(object sender, EventArgs e)
{
r.Name = textBox1.Text;
}
}
}
- remoting教学四:remoting的事件
- remoting教学二:remoting的激活
- remoting教学三:remoting的远程对象
- Remoting的事件机制
- Remoting 事件
- remoting
- Remoting
- Remoting
- remoting
- remoting
- Remoting
- Remoting
- Remoting
- Remoting
- Remoting
- Remoting
- remoting教学一:remoting与socket、webservice和wcf的比较及优势
- Remoting远程处理事件调用的问题
- JS取出今天得日期时间
- IHTMLDocument Interface
- Hibernate及Spring quartz开发中几个注意点
- remoting教学三:remoting的远程对象
- window.open打开窗口时父窗口变成object
- remoting教学四:remoting的事件
- 场景的功能
- Javascript----文件操作
- remoting草稿
- 详解批处理命令
- remoting安全之草稿
- ORACLE SQL优化 - ORACLE执行计划
- ASP.NET 2.0中的跨页面提交
- json