Click here to Skip to main content
15,868,027 members
Articles / Desktop Programming / Windows Forms

A Remoting Event (Simple and Efficient for Enterprise Solutions)

Rate me:
Please Sign up or sign in to vote.
4.84/5 (25 votes)
21 Aug 2006CPOL2 min read 161.1K   2.1K   85  
This article contains the simplest solutions for: the security problem for DelegateSerializationHolder, the IO problem, and the messaging speed problem. Note: Messaging speed problem will appear when your application has worked for a long time.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using Remotable;
using System.Runtime.Remoting.Messaging;

namespace Client
{
    public partial class ClientForm : Form
    {
        public ClientForm()
        {
            InitializeComponent();
        }

        RemoteClass remoteClass;
        WrapperClass wrapperClass;

        private void Form1_Load(object sender, EventArgs e)
        {
            //Configure remoting.
            RemotingConfiguration.Configure(Application.StartupPath + "\\Client.exe.config",false);

            // Create a proxy from remote object.
            remoteClass = (RemoteClass)Activator.GetObject(typeof(RemoteClass), "http://localhost:8080/Chat");
            //Create an instance of wrapper class.
            wrapperClass = new WrapperClass();
          
            //Associate remote object event with wrapper method.
            remoteClass.MessageReceived += new MessageHandler(wrapperClass.WrapperMessageReceivedHandler);
            //Associate wrapper event with current form event handler.
            wrapperClass.WrapperMessageReceived += new MessageHandler(MessageReceivedHandler);
        }
        /// <summary>
        /// Sends message.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSend_Click(object sender, EventArgs e)
        {
            remoteClass.Send(textBoxMessage.Text);
        }
        
        /// <summary>
        /// Receive message.
        /// </summary>
        /// <param name="message"></param>
        public void MessageReceivedHandler(string message)
        {
            if (listBoxReceived.InvokeRequired == false)
            {
                listBoxReceived.Items.Add(message);
            }
            else
            {
                // Show the text asynchronously
                MessageHandler statusDelegate =
                   new MessageHandler(crossThreadEventHandler);
                this.BeginInvoke(statusDelegate,
                  new object[] { message });
            }
            
        }

        /// <summary>
        /// Asynchronously called method to show message in the UI.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void crossThreadEventHandler(string message)
        {
            listBoxReceived.Items.Add(message);
        }

        /// <summary>
        /// Unassociate event handlers.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                remoteClass.MessageReceived -= new MessageHandler(wrapperClass.WrapperMessageReceivedHandler);
                wrapperClass.WrapperMessageReceived -= new MessageHandler(MessageReceivedHandler);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        
    }

   

}

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