Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Remoting Event (Simple and Efficient for Enterprise Solutions)

, 21 Aug 2006 CPOL
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)

Share

About the Author

Hossein Ghahvei Araghi
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
Follow on   Twitter   Google+

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 21 Aug 2006
Article Copyright 2006 by Hossein Ghahvei Araghi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid