For using remoting with c#
In this example create a class library and add the following code. My project name is RemotableObject. Note your project name because you need to refer this in your client and server projects
[Serializable]
public class Remotable : MarshalByRefObject
{
public static Delegate setListView;
public void Test(ArrayList list)
{
object[] args = new object[1];
args[0] = list;
setListView.DynamicInvoke(args);
}
}
Compile this class library in to the dll file.
Now create a server project, add a listview (say listview1) in the form (simple listview for this example, no sublist).
Add refernce to the dll file created.The namespce for my example is using RemotableObject;
You need to add also the System.Runtime.Remoting (Add reference-> in the .Net tab select this and click ok)
Add a button and in the button's click event...
private void button1_Click(object sender, EventArgs e)
{
try
{
ChannelServices.RegisterChannel(new TcpChannel(), false);
}
catch { }
Type requiredType = typeof(Remotable);
Remotable remoteObject = (Remotable)Activator.GetObject(requiredType, "tcp://localhost:9998/ListService");
ArrayList list = new ArrayList();
foreach (ListViewItem item in listView1.Items)
{
list.Add(item.Text);
}
remoteObject.Test(list);
remoteObject = null;
}
Note: use these additional libraries for server and as well as for client
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotableObject;
In the client form add a list view anf have the following code
You need to add also the System.Runtime.Remoting (Add reference-> in the .Net tab select this and click ok)
Add the reference to the dll to this project as well and have the above references..
public partial class Form1 : Form
{
private delegate void setList(ArrayList list);
private ArrayList list;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TcpChannel tcpChannel = new TcpChannel(9998);
ChannelServices.RegisterChannel(tcpChannel, false);
Type commonInterfaceType = typeof(Remotable);
RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType,
"ListService", WellKnownObjectMode.SingleCall);
Remotable.setListView = new setList(setListView);
}
public void setListView(ArrayList list)
{
this.list = list;
if (listView1.InvokeRequired)
{
listView1.Invoke(new MethodInvoker(invoker));
}
}
public void invoker()
{
foreach (String listItemText in this.list)
{
ListViewItem item = new ListViewItem(listItemText);
listView1.Items.Add(item);
}
listView1.Refresh();
}
}
;
START the client first, because client listens and server access it execute its method to update the listview.
Then run the server....
click the button (we added earlier) which has the remoting code at its event. Now you can see the listview in the client get updated