none
通过循环和事件驱动实现的顺序工作流的序列化和反序列化? RRS feed

  • 问题

  •  

    顺序工作流中通过循环和事件驱动,实现多个“状态”间的跳转。在此工作流中如何序列化和如何加载已序列化的工作流?

    设计代码

    程序块

    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Collections;
    using System.Drawing;
    using System.Reflection;
    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 MaterialWorkflow
    {
     partial class MaterialSequentialWorkflow
     {
      #region Designer generated code
      
      ///
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      ///
            [System.Diagnostics.DebuggerNonUserCode]
      private void InitializeComponent()
      {
                this.CanModifyActivities = true;
                System.Workflow.Activities.CodeCondition codecondition1 = new System.Workflow.Activities.CodeCondition();
                this.GoBack = new System.Workflow.Activities.CodeActivity();
                this.MatreialManagerAgree = new System.Workflow.Activities.HandleExternalEventActivity();
                this.GoNext = new System.Workflow.Activities.CodeActivity();
                this.MaterialNext = new System.Workflow.Activities.HandleExternalEventActivity();
                this.eventDrivenActivity2 = new System.Workflow.Activities.EventDrivenActivity();
                this.eventDrivenActivity1 = new System.Workflow.Activities.EventDrivenActivity();
                this.listenActivity1 = new System.Workflow.Activities.ListenActivity();
                this.faultHandlersActivity1 = new System.Workflow.ComponentModel.FaultHandlersActivity();
                this.whileActivity1 = new System.Workflow.Activities.WhileActivity();
                //
                // GoBack
                //
                this.GoBack.Name = "GoBack";
                this.GoBack.ExecuteCode += new System.EventHandler(this.GoBack_ExecuteCode);
                //
                // MatreialManagerAgree
                //
                this.MatreialManagerAgree.EventName = "no";
                this.MatreialManagerAgree.InterfaceType = typeof(MaterialWorkflow.MaterialService);
                this.MatreialManagerAgree.Name = "MatreialManagerAgree";
                //
                // GoNext
                //
                this.GoNext.Name = "GoNext";
                this.GoNext.ExecuteCode += new System.EventHandler(this.GoNext_ExecuteCode);
                //
                // MaterialNext
                //
                this.MaterialNext.EventName = "yes";
                this.MaterialNext.InterfaceType = typeof(MaterialWorkflow.MaterialService);
                this.MaterialNext.Name = "MaterialNext";
                //
                // eventDrivenActivity2
                //
                this.eventDrivenActivity2.Activities.Add(this.MatreialManagerAgree);
                this.eventDrivenActivity2.Activities.Add(this.GoBack);
                this.eventDrivenActivity2.Name = "eventDrivenActivity2";
                //
                // eventDrivenActivity1
                //
                this.eventDrivenActivity1.Activities.Add(this.MaterialNext);
                this.eventDrivenActivity1.Activities.Add(this.GoNext);
                this.eventDrivenActivity1.Name = "eventDrivenActivity1";
                //
                // listenActivity1
                //
                this.listenActivity1.Activities.Add(this.eventDrivenActivity1);
                this.listenActivity1.Activities.Add(this.eventDrivenActivity2);
                this.listenActivity1.Name = "listenActivity1";
                //
                // faultHandlersActivity1
                //
                this.faultHandlersActivity1.Name = "faultHandlersActivity1";
                //
                // whileActivity1
                //
                this.whileActivity1.Activities.Add(this.listenActivity1);
                codecondition1.Condition += new System.EventHandler(this.IsStop);
                this.whileActivity1.Condition = codecondition1;
                this.whileActivity1.Name = "whileActivity1";
                //
                // MaterialSequentialWorkflow
                //
                this.Activities.Add(this.whileActivity1);
                this.Activities.Add(this.faultHandlersActivity1);
                this.Name = "MaterialSequentialWorkflow";
                this.CanModifyActivities = false;

      }

      #endregion

            private CodeActivity GoBack;
            private CodeActivity GoNext;
            private HandleExternalEventActivity MatreialManagerAgree;
            private HandleExternalEventActivity MaterialNext;
            private EventDrivenActivity eventDrivenActivity2;
            private EventDrivenActivity eventDrivenActivity1;
            private ListenActivity listenActivity1;
            private WhileActivity whileActivity1;
            private FaultHandlersActivity faultHandlersActivity1;
        }
    }

     

     

    自定义服务代码

    程序块

    using System;
    using System.Threading;
    using System.Workflow.ComponentModel;
    using System.Workflow.Runtime;
    using System.Workflow.Runtime.Hosting;
    using System.Workflow.Activities;
    using System.Windows.Forms;
    using System.Data;

    namespace MaterialWorkflow
    {
        [Serializable]
        public class MaterialEventArgs : ExternalDataEventArgs
        {
            public MaterialEventArgs(Guid InstanceID) : base(InstanceID)
            {
            }
        }

        [ExternalDataExchange]
        public interface MaterialService
     {
            event EventHandler<ExternalDataEventArgs> yes;
            event EventHandler<ExternalDataEventArgs> no;
     }
        [Serializable]
        public class IMaterialService : MaterialService
        {
            public event EventHandler<ExternalDataEventArgs> yes;
            public event EventHandler<ExternalDataEventArgs> no;

            public void Next(Guid instanceId )
            {
                ThreadPool.QueueUserWorkItem(GoNext,
                    new MaterialEventArgs(instanceId));
            }

            public void Back(Guid instanceId)
            {
                ThreadPool.QueueUserWorkItem(GoBack,
                    new MaterialEventArgs(instanceId));
            }

            public void GoNext(object o)
            {
                MaterialEventArgs e = (MaterialEventArgs)o;
                if (yes != null)
                {
                    yes(null, e);
                }
                else
                {
                    MessageBox.Show("到下一个流程事件为空!");
                }
            }

            public void GoBack(object o)
            {
                MaterialEventArgs e = (MaterialEventArgs)o;
                if (no != null)
                {
                    no(null, e);
                }
                else
                {
                    MessageBox.Show("返回流程事件为空!");
                }
            }
     
        }
    }

     

     

    程序代码

    程序块

    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 MaterialWorkflow
    {
        public sealed partial class MaterialSequentialWorkflow : SequentialWorkflowActivity
        {
            private MaterialState _CurrentState = MaterialState.Apply;
            public MaterialState CurrentState
            {
                get { return _CurrentState; }
                set { _CurrentState = value; }
            }


            private bool _IsDirector;
            ///
            /// 判断是否是由主任批准。
            ///
            public bool IsDirector
            {
                get { return _IsDirector; }
                set { _IsDirector = value; }
            }

            public MaterialSequentialWorkflow()
            {
                InitializeComponent();
            }

            private void StartApply(object sender, ConditionalEventArgs e)
            {
                e.Result = false;
            }

            private void IsStop(object sender, ConditionalEventArgs e)
            {
                if (CurrentState == MaterialState.Over)
                {
                    e.Result = false;
                }
                else
                {
                    e.Result = true;
                }
            }

            private void GoNext_ExecuteCode(object sender, EventArgs e)
            {
                if (Convert.ToInt32(CurrentState) < Convert.ToInt32(MaterialState.Over))
                {
                    if (IsDirector)
                    {
                        CurrentState = (MaterialState)(Convert.ToInt32(CurrentState) + 1);
                    }
                    else
                    {
                        if (CurrentState == MaterialState.Apply)
                        {
                            CurrentState = MaterialState.FillTable;
                        }
                        else
                        {
                            CurrentState = (MaterialState)(Convert.ToInt32(CurrentState) + 1);
                        }
                    }
                }
            }

            private void GoBack_ExecuteCode(object sender, EventArgs e)
            {
                if (Convert.ToInt32(CurrentState) > 0)
                {
                    if (IsDirector)
                    {
                        CurrentState = (MaterialState)(Convert.ToInt32(CurrentState) - 1);
                    }
                    else
                    {
                        if (CurrentState == MaterialState.FillTable)
                        {
                            CurrentState = MaterialState.Apply;
                        }
                        else
                        {
                            CurrentState = (MaterialState)(Convert.ToInt32(CurrentState) - 1);
                        }
                    }
                }
            }
        }

    //状态

        public enum MaterialState
        {
            Apply,
            Checker,
            Director,
            MaterialManager,
            StoreroomManager,
            FillTable,
            TechnologyOffice,
            ProductDirector,
            MaterialDirector,
            Get,
            Over
        }
    }

     

     


    • 已移动 小鱼儿 2009年5月19日 10:32 ([Loc]From:.NET Framework 相关)
    2007年12月29日 3:05