Code for Foundations (June 2007)
Foundations
ActivityExecutionContext in Workflows
Matt Milner
Navigate: [root] / MSDNMag.WFActivities / ReadLine.cs
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace MSDNMag.WFActivities
{
    [Designer(typeof(ConsoleDesigner), typeof(IDesigner))]
    [ToolboxBitmap(typeof(ReadLine), "Resources.icon_exe.png")]
    public partial class ReadLine : System.Workflow.ComponentModel.Activity, IActivityEventListener<QueueEventArgs>, IEventActivity
	{
        private string qName;

		public ReadLine()
		{
			InitializeComponent();
		}

        public static DependencyProperty TextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Text", typeof(string), typeof(ReadLine));

        [Description("The text read in from the console")]
        [Category("Console")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Text
        {
            get
            {
                return ((string)(base.GetValue(ReadLine.TextProperty)));
            }
            set
            {
                base.SetValue(ReadLine.TextProperty, value);
            }
        }

        protected override void Initialize(IServiceProvider provider)
        {
            base.Initialize(provider);
            qName = this.QualifiedName;
        }
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            
            if (ProcessMessage(executionContext))
            {
                return ActivityExecutionStatus.Closed;
            }
            else
            {
                Subscribe(executionContext, this);
                return ActivityExecutionStatus.Executing;
            }
        }

        private bool ProcessMessage(ActivityExecutionContext provider)
        {
            WorkflowQueuingService qService = provider.GetService<WorkflowQueuingService>();

            if (qService.Exists(this.QueueName) && qService.GetWorkflowQueue(this.QueueName).Count > 0)
            {
                object input = qService.GetWorkflowQueue(this.QueueName).Dequeue();

                if (input == null)
                    return false;
                else
                {
                    Text = input.ToString();
                    return true;
                }
            }
            return false;
        }

        #region IActivityEventListener<QueueEventArgs> Members

        public void OnEvent(object sender, QueueEventArgs e)
        {
            ActivityExecutionContext ctx = sender as ActivityExecutionContext;
            if (ProcessMessage(ctx))
            {
                Unsubscribe(ctx, this);
                ctx.CloseActivity();
            }
        }

        #endregion

        protected override void Uninitialize(IServiceProvider provider)
        {
            base.Uninitialize(provider);


        }

        #region IEventActivity Members

        public IComparable QueueName
        {
            get { return qName; }
        }

        public void Subscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)
        {
            WorkflowQueuingService qService = parentContext.GetService<WorkflowQueuingService>();

            if (qService != null && !qService.Exists(this.QueueName))
            {
                WorkflowQueue q= qService.CreateWorkflowQueue(this.QueueName, false);
                q.RegisterForQueueItemAvailable(parentEventHandler);
                IReadLineService readService = parentContext.GetService<IReadLineService>();
                if (readService != null)
                    readService.ReadLine(this.QueueName);

            }
        }

        public void Unsubscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)
        {
            WorkflowQueuingService qService = parentContext.GetService<WorkflowQueuingService>();


            if (qService != null && qService.Exists(this.QueueName))
            {
                qService.GetWorkflowQueue(this.QueueName).UnregisterForQueueItemAvailable(parentEventHandler);
                qService.DeleteWorkflowQueue(this.QueueName);
            }
        }

        #endregion
    }
}
 NameSize
Properties3 Items
Resources1 Item
AECReporterActivity.cs1 KB
AECReporterActivity.Designer.cs970 Bytes
ConsoleDesigner.cs701 Bytes
ConsoleReadLineService.cs943 Bytes
ForEach.cs5 KB
ForEach.Designer.cs1 KB
ForEachActivityDesigner.cs761 Bytes
ForEachActivityValidator.cs1015 Bytes
IReadLineService.cs192 Bytes
MSDNMag.WFActivities.csproj3 KB
MSDNMag.WFActivities.csproj.user168 Bytes
ReadLine.cs4 KB
ReadLine.Designer.cs946 Bytes
WriteLine.cs2 KB
WriteLine.Designer.cs949 Bytes
WriteLineValidator.cs781 Bytes
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker