![]() |
General Programming »
Internet / Network »
Remoting
Intermediate
Using Windows Controls Remotely With .NET RemotingBy Hossein eraghiThe article describes how to share windows control functionality using .NET Remoting |
C# 2.0, Windows, .NET 2.0, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

In this article you will find a very interesting note about the way that you can share any kind of Windows control functionality from the server application to the client application.
For simplicity purpose you will find no complex code in the source code.
In the past if you shared a class with the .NET Remoting you would certainly know these notes:
RemotingServices class
So, the code consists of three projects:
RemoteLib is a class library project and is the shared library.It contains two classes:
RemotableClass is the shared class. SharedResourcesHandler class contains shared controls in the static property form.RemoteServer is a windows application project and is the hosting application for the RemoteLib library.RemoteClient is a windows application project and is the client application for the RemoteLib library./// SharedResourcesHandler Class ///
private static TextBox innerTextBox;
/// <summary>
/// Gets or sets the shared TextBox Control in the RemoteLib.
/// </summary>
public static TextBox InnerTextBox
{
get { return innerTextBox; }
set { innerTextBox = value; }
}
private static MonthCalendar innerMonthCalendar;
/// <summary>
/// Gets or sets the shared innerMonthCalendar Control in the RemoteLib.
/// </summary>
public static MonthCalendar InnerMonthCalendar
{
get { return innerMonthCalendar; }
set { innerMonthCalendar = value; }
}
Any kind of .NET controls could be used Instead of TextBox and MonthCalendar to be shared.
In the RemotableClass class you will use these controls to set properties.
Indeed the RemotableClass class is a wrapper class for publishing controls functionality to the clients.
/// <summary>
/// Represents the method that will handle cross thread calls.
/// </summary>
/// <param name="text"></param>
private delegate void UIThreadHandler<T>(T param);
/// <summary>
/// Initializes a new instance of the RemotableClass
/// </summary>
public RemotableClass()
{
}
/// <summary>
/// Obtains a lifetime service object to control the lifetime policy for
/// this instance.
/// </summary>
/// <returns>
/// An object of type System.Runtime.Remoting.Lifetime.ILease used to
/// control the lifetime policy for this instance. This is the current
/// lifetime service object for this instance if one exists; otherwise, a
/// new lifetime service object initialized to the value of the
/// System.Runtime.Remoting.Lifetime.LifetimeServices.LeaseManagerPollTime
/// property.
/// </returns>
public override object InitializeLifetimeService()
{
ILease lease = (ILease)base.InitializeLifetimeService();
if (lease.CurrentState == LeaseState.Initial)
{
lease.InitialLeaseTime = TimeSpan.FromMinutes(10);
}
return lease;
}
/// <summary>
/// Writes specified text to the TextBox Control.
/// </summary>
/// <param name="text"></param>
public void WriteText(string text)
{
if (SharedResourcesHandler.InnerTextBox != null)
{
UIThreadHandler<string> uiThreadHandler =
new UIThreadHandler<string>(AsyncWriteText);
if (SharedResourcesHandler.InnerTextBox.InvokeRequired)
SharedResourcesHandler.InnerTextBox.BeginInvoke(uiThreadHandler,
text);
else
SharedResourcesHandler.InnerTextBox.Text = text;
}
}
/// <summary>
/// Writes specified text to the TextBox Control in the UI thread.
/// </summary>
/// <param name="text"></param>
private void AsyncWriteText(string text)
{
SharedResourcesHandler.InnerTextBox.Text = text;
}
/// <summary>
/// Changes the MonthCalendar date to specified date.
/// </summary>
/// <param name="text"></param>
public void ChangeDate(DateTime date)
{
if (SharedResourcesHandler.InnerMonthCalendar != null)
{
UIThreadHandler<DateTime> uiThreadHandler =
new UIThreadHandler<DateTime>(AsyncChangeDate);
if (SharedResourcesHandler.InnerMonthCalendar.InvokeRequired)
SharedResourcesHandler.InnerMonthCalendar.BeginInvoke(
uiThreadHandler, date);
else
SharedResourcesHandler.InnerMonthCalendar.SetDate(date);
}
}
/// <summary>
/// Changes the MonthCalendar date to specified date in the UI thread.
/// </summary>
/// <param name="text"></param>
private void AsyncChangeDate(DateTime date)
{
SharedResourcesHandler.InnerMonthCalendar.SetDate(date);
}
Changing properties of shared controls must be a cross thread call, and done with a middle generic delegate named UIThreadHandler
So where is the control when you are filling static members of the SharedResourcesHandler class?
The answer is one line per control in the server application like this:
//Assigning Control from UI to static member of SharedResourcesHandler class
SharedResourcesHandler.InnerTextBox = this.txtRemote;
SharedResourcesHandler.InnerMonthCalendar = this.mcRemote;
Remaining server code is for Registering RemotableClass for enabling remote access
TcpServerChannel channel = new TcpServerChannel(8080);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.ApplicationName = "RemotableClass";
RemotingConfiguration.RegisterWellKnownServiceType(typeof(
RemoteLib.RemotableClass), "RemotableClass",
WellKnownObjectMode.SingleCall);
Also you can use the Remoting section in the server configuration instead of preceding code.
In the client application the first step is getting remote object with this:
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel,false);
rClass = (RemotableClass)Activator.GetObject(typeof(RemotableClass),
"tcp://127.0.0.1:8080/RemotableClass");
I used localhost for testing purpose you can use any valid IP Addresses instead.
Also you can use configuration file to configure remoting enabled application.
Second step is calling shared object wrapper methods with events in the client application.
/// <summary>
/// Calling WriteText method of RemotableClass
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtRemote_TextChanged(object sender, EventArgs e)
{
rClass.WriteText(txtRemote.Text);
}
/// <summary>
/// Calling ChangeDate method of RemotableClass
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mcRemote_DateChanged(object sender, DateRangeEventArgs e)
{
rClass.ChangeDate(mcRemote.SelectionStart);
}
This is a test application, in your real application you must use this trick object oriented.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 3 Apr 2007 Editor: Sean Ewington |
Copyright 2007 by Hossein eraghi Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |