Click here to Skip to main content
15,891,316 members
Articles / Programming Languages / C#

Host and Workflow: Two Worlds to Communicate IV

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
3 Oct 2008CPOL9 min read 32.4K   418   15  
Part IV: Organisation of the communication classes: Communication manager, wca.exe utility and Wwca.exe windows front-end for wca.exe
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Workflow.Runtime;
using jagg.ClassCommunication;
using UsingWCAInWF;

namespace MultiValve
{
	public partial class Valve: UserControl
	{
        WorkflowInstance wi = null;
        WorkflowRuntime wr = null;
        CommunicationManager cm = null;

        #region Event to advice that the valve is closed!

        public event EventHandler<EventArgs> ValveChange;


        protected void OnValveChange(object sender, EventArgs e)
        {
            if (ValveChange != null)
            {
                ValveChange(sender, e);
            }
        }
        #endregion

        #region properties
        /// <summary>
        /// Give true is the valve is closed
        /// </summary>
        public bool IsClosed
        {
            get
            {
                return (bControl.BackColor == Color.DarkGreen);
            }

        }
        #endregion

        #region Initialization
        /// <summary>
        /// Constructor
        /// </summary>
        public Valve()
		{
			InitializeComponent();
            lName.Text = "-";
		}
        /// <summary>
        /// Initialization of activities
        /// </summary>
        /// <param name="name">Decorative name for the valve</param>
        /// <param name="wr">runtime reference</param>
        /// <param name="cm">communication manager reference</param>
        public void InitializeControl(string name, WorkflowRuntime wr, CommunicationManager cm)
        {
            wi = null;
            wi = wr.CreateWorkflow(typeof(WFValveControl));
            this.wr = wr;
            this.cm = cm;
            lName.Text = name;
            cm.Valve.EventValveStatus += new EventHandler<ExternalValveEventArgs>(Valve_EventValveStatus);
            wi.Start();
        }

       #endregion

        #region WorkFLow to Host response
        void Valve_EventValveStatus(object sender, ExternalValveEventArgs e)
        {
            
            if (e.InstanceId == wi.InstanceId)
            {
                IAsyncResult result = this.BeginInvoke(
                    new EventHandler(
                    delegate
                    {
                        //Process Event
                        switch (e.Status)
                        {
                            case "CLOSE":
                                {
                                    bControl.BackColor = Color.DarkGreen;
                                    OnValveChange(this, e); //You need that to controle active button                   
                                    break;
                                }
                            case "OPEN":
                                {
                                    bControl.BackColor = Color.LightGreen;
                                    OnValveChange(this, e); //You need that to controle active button                   
                                    break;
                                }
                            case "EXIT": { bControl.BackColor = Color.DarkGray; break; }
                        }
                    }));
                this.EndInvoke(result);
            }
        }

        #endregion

        public void ExitControl()
        {
            cm.Valve.RaiseExit(wi.InstanceId);
        }
        /// <summary>
        /// Send order to Workflow
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bControl_Click(object sender, EventArgs e)
        {
            string dg = Color.DarkGreen.Name;
            switch (bControl.BackColor.Name)
            { 
                case "DarkGreen": {cm.Valve.RaiseOpen(wi.InstanceId); break; }
                case "LightGreen":{cm.Valve.RaiseClose(wi.InstanceId); break; }
            }
        }


    }
}

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) Avalon Development
United States United States
Jose A. Garcia Guirado, Electronic Engineer, graduated in Havana/Cuba 1982, MCTS, MCSD.NET, MCAD.NET, MCSE. Worked in the Institute for Cybernetics and Mathematics of Academy of Science of Cuba for 8 years; since 1995 working as free software architect, developer and adviser, first in Argentina and from 2003 to 2010, in Germany as External consultant in DWS Luxembourg, AIXTRON AG and Shell Deutschland GmbH and from 2010 to 2012 in Mexico working for Twenty Century Fox, and Mexico Stock Exchange (BMV). From 2013 to now in USA, Florida, First in FAME Inc. and now as Senior Software Engineer in Spirit Airlines.

Comments and Discussions