VS2008 WorkFlow开发

应用程序中调用工作流:

using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                
                AutoResetEvent waitHandle = new AutoResetEvent(false);              
                workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { Console.WriteLine("WorkFlow Completed!"); };
                workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    //waitHandle.Set();
                };

//为工作流添加参数
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("Alias", "lxf");

//为工作流添加数据交换服务

                ExternalDataExchangeService dataService = new ExternalDataExchangeService();
                workflowRuntime.AddService(dataService);

//实例化服务内容
                VotingServiceImpl votingservice = new VotingServiceImpl();
                
                dataService.AddService(votingservice);
                workflowRuntime.StartRuntime();
//调用工作流
                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowConsoleApplication1.Workflow1),parameters);
                
                instance.Start();

                waitHandle.WaitOne();
            }
            Console.ReadLine();
        }

2、定义工作流服务接口

[ExternalDataExchange]
    internal interface IVotingServices
{
        event EventHandler<VotingServiceEventArgs> ApprovedProposal;
        event EventHandler<VotingServiceEventArgs> RejectProposal;
        void CreateBallot(string alias);
}

3、定义工作流服务事件参数

[Serializable]
    internal class VotingServiceEventArgs : ExternalDataEventArgs
    {
        private string aliasvalue;

        public VotingServiceEventArgs(Guid instanceID, string alias)
            : base(instanceID)
        {
            this.aliasvalue = alias;
        }

        public string Alias
        {
            get
            {
                return this.aliasvalue;
            }
        }
    }

4、定义工作流服务实现类

internal class VotingServiceImpl : IVotingServices
    {
        public event EventHandler<VotingServiceEventArgs> ApprovedProposal;
        public event EventHandler<VotingServiceEventArgs> RejectProposal;

        public void CreateBallot(string alias)
        {
            Console.WriteLine("Bollot created for {0}.", alias);
            ShowVotingDialog(new VotingServiceEventArgs(WorkflowEnvironment.WorkflowInstanceId,alias));
        }

        public void ShowVotingDialog(VotingServiceEventArgs votingEventArgs)
        {
            DialogResult result;
            string alias = votingEventArgs.Alias;

            result = MessageBox.Show(string.Format("Approved Proposal,{0}?",alias),string.Format("{0} Bollot",alias),MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                EventHandler<VotingServiceEventArgs> approvedProposal = this.ApprovedProposal;
                if (approvedProposal != null)
                    approvedProposal(null, votingEventArgs);
            }
            else
            {
                EventHandler<VotingServiceEventArgs> rejectProposal = this.RejectProposal;
                if (rejectProposal != null)
                    rejectProposal(null, votingEventArgs);
            }
        }
    }

5、开发工作流

1)添加callExternalMethodActivity,为其设置接口 IVotingServices,事件CreateBallot
2)添加listenActivity
3)为listenActivity添加handleExternalEventActivity1,handleExternalEventActivity2,分别定义
接口 IVotingServices,事件为ApprovedProposal,RejectProposal

至此,简单的例子已经完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值