Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Using Windows Controls Remotely With .NET Remoting

Rate me:
Please Sign up or sign in to vote.
4.08/5 (6 votes)
3 Apr 2007CPOL2 min read 35.5K   967   21  
The article describes how to share windows control functionality using .NET Remoting
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Lifetime;

namespace RemoteLib
{
    /// <summary>
    /// Represents shared class for use in the client and the server applications.
    /// </summary>
    public class RemotableClass:MarshalByRefObject
    {

        /// <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);
        }
       

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager System Group
Iran (Islamic Republic of) Iran (Islamic Republic of)
Hossein Ghahvei Araghi
Birth date: 1978
Birth place: Iran
Academic Credentials : BS(Tehran University)
Microsoft Credentials : MCP, MCAD, MCTS 2.0, MCTS 3.5, MCPD 2.0, MCPD 3.5

Comments and Discussions