⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controller.cs

📁 WF本质论一书的源码,要书籍的朋友同我联系.
💻 CS
字号:
using System;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;

namespace EssentialWF.Activities {

    public class Controller : CompositeActivity  {
        protected override void Initialize(IServiceProvider provider){
            WorkflowQueuingService qService = provider.GetService(typeof(WorkflowQueuingService)) as WorkflowQueuingService;
            WorkflowQueue queue = qService.CreateWorkflowQueue(this.Name, false);
        }
        protected override void Uninitialize(IServiceProvider provider) {
            WorkflowQueuingService qService = provider.GetService(typeof(WorkflowQueuingService)) as WorkflowQueuingService;
            qService.DeleteWorkflowQueue(this.Name);
        }
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext context) {
            WorkflowQueuingService qService = context.GetService(typeof(WorkflowQueuingService)) as WorkflowQueuingService;
            WorkflowQueue queue = qService.GetWorkflowQueue(this.Name);
            queue.QueueItemAvailable += this.ResumeAt;

            return ActivityExecutionStatus.Executing;
        }

        void ResumeAt(object sender, QueueEventArgs e) {
            ActivityExecutionContext context = sender as ActivityExecutionContext;
            WorkflowQueuingService qService = context.GetService(typeof(WorkflowQueuingService)) as WorkflowQueuingService;
            WorkflowQueue queue = qService.GetWorkflowQueue(this.Name);

            string s = (string)queue.Dequeue();

            if (s != null) {
                if (s.StartsWith("execute")) {
                    // command will be something like 'execute w1'
                    string[] tokens = s.Split(new char[] { ' ' });
                    string name = tokens[1];
                    Activity child = GetChildActivity(name);
                    if (child != null) {
                        ActivityExecutionContextManager manager = context.ExecutionContextManager;
                        ActivityExecutionContext c = manager.CreateExecutionContext(child);

                        c.Activity.Closed += this.ContinueAt;
                        c.ExecuteActivity(c.Activity);
                    }
                }
            }
        }

        void ContinueAt(object sender, ActivityExecutionStatusChangedEventArgs e) {
            ActivityExecutionContext context = sender as ActivityExecutionContext;
            ActivityExecutionContextManager manager = context.ExecutionContextManager;
            ActivityExecutionContext c = manager.GetExecutionContext(e.Activity);
            manager.CompleteExecutionContext(c, false);
        }

        private Activity GetChildActivity(string name) {
            foreach (Activity child in EnabledActivities) {
                if (child.Name.Equals(name))
                    return child;
                }
                return null;
            }
        // Cancellation logic
        //    ...
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -