Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C#

Communication options with WCF - Part 1

Rate me:
Please Sign up or sign in to vote.
4.84/5 (93 votes)
11 Jul 2006CPOL15 min read 326.9K   1.8K   262  
Communication options with WCF - Part 1.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.Messaging;
using System.Configuration;
using System.Collections;

namespace QState
{
    public partial class QStateWnd : Form
    {
        ArrayList userOptions;
        ServiceHost host = null;
        QSMonitor handler;

        public QStateWnd()
        {
        //           To create the queue programmatically uncomment these lines.
        //           string queueName = ConfigurationManager.AppSettings["queueName"];
        //           if (!MessageQueue.Exists(queueName))
        //               MessageQueue.Create(queueName, false);

            handler = new QSMonitor(this);

            // Get base address for Msmq
            string baseAddress = ConfigurationManager.AppSettings["baseAddress"];
            host = new ServiceHost(handler, new Uri(baseAddress));
            // Open the ServiceHostBase to create listeners and start listening for messages.
            host.Open();

            userOptions = new ArrayList();  //in case we want to persist...
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strMod"></param>
        /// <param name="strState"></param>
        public void ProcessMessage(string strMod, string strState)
        {
            //Check to see if this sender is enabled
            bool boolEnabled = true;
            if (IsModuleKnown(strMod, out boolEnabled) == false)
            {
                //We haven't seen this one before so always create it the first time
                userOptions.Add(new UserOptionItem(strMod, true));
                ModuleState wnd = new ModuleState();
                wnd.SenderModule = strMod;
                wnd.AddMessage(strState);
                wnd.MdiParent = this;
                wnd.Show();
            }
            else
            {
                if (boolEnabled)
                {
                    ModuleState wnd = null;
                    bool boolWndExists = DoesWndExist(strMod, out wnd);
                    if (boolWndExists)
                        wnd.AddMessage(strState);
                    else
                    {
                        ModuleState wnd2 = new ModuleState();
                        wnd2.SenderModule = strMod;
                        wnd2.AddMessage(strState);
                        wnd2.MdiParent = this;
                        wnd2.Show();
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="enabled"></param>
        /// <returns></returns>
        private bool IsModuleKnown(string sender, out bool enabled)
        {
            bool boolReturn = false;
            enabled = false;

            foreach (UserOptionItem i in userOptions)
                if (i.modName == sender)
                {
                    enabled = i.enabled;
                    boolReturn = true;
                    break;
                }
            return boolReturn;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="wnd"></param>
        /// <returns></returns>
        private bool DoesWndExist(string sender, out ModuleState wnd)
        {
            wnd = null;
            bool boolWndExist = false;

            System.Collections.IEnumerator e = this.MdiChildren.GetEnumerator();

            while ((e.MoveNext()) && (e.Current != null))
            {
                if (((ModuleState)e.Current).SenderModule == sender)
                {
                    boolWndExist = true;
                    wnd = (ModuleState)e.Current;
                    break;
                }
            }
            return boolWndExist;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QStateWnd_FormClosing(object sender, FormClosingEventArgs e)
        {
            host.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(System.Windows.Forms.MdiLayout.TileHorizontal);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void modulesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Only if there are some
            if (userOptions.Count > 0)
            {
                UserOption dlg = new UserOption();
                dlg.userOptions = userOptions;
                dlg.ShowDialog();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Collections.IEnumerator en = this.MdiChildren.GetEnumerator();
            while ((en.MoveNext()) && (en.Current != null))
                ((ModuleState)en.Current).ClearAll();
        }
    }
}

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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions