Windows Workflow(VS 2019)

一、启动流程多种方式

1)调用活动(activity),假设通过新增项新建活动为Activity1,具体工作流程可以在工作流程编辑界面进行编辑。

using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;

namespace WorkflowConsoleApplication2
{

    class Program
    {
        static void Main(string[] args)
        {
            Activity workflow1 = new Activity1();
            WorkflowInvoker.Invoke(workflow1);
            Console.ReadLine();
  
        }
    }
}

2)直接代码声明一个活动,参考官网代码:

// Define the workflow  
Activity wf = new Sequence  
{  
    Variables = { var1, var2, var3 },  
    Activities =
    {  
        new Assign<string>()  
        {  
            Value = new InArgument<string>(var1),  
            To = new OutArgument<string>(var2)  
        },  
        new WriteLine() { Text = var2 },  
        new Assign<string>()  
        {  
            Value = var2,  
            To = var3  
        },  
        new WriteLine() { Text = var3 }  
    }  
};  
  
WorkflowInvoker.Invoke(wf);  

3)调用流程定义的xaml文件

 

二、变量和参数

流程中定义的变量作用于整个流程,codeactivity活动中定义输入输出变量分别为runnumber和modirunnumber,可以从流程变量中获得输入变量值,然后再将计算后的值赋予输出变量。

 public sealed class CodeActivityDealArguments : CodeActivity
    {
        // 定义一个字符串类型的活动输入参数
        public InArgument<int> runnumber { get; set; }
        public OutArgument<int> modirunnumber { get; set; }
        // 如果活动返回值,则从 CodeActivity<TResult>
        // 并从 Execute 方法返回该值。
        protected override void Execute(CodeActivityContext context)
        {
            // 获取 Text 输入参数的运行时值
            int text = context.GetValue(this.runnumber);
            Console.WriteLine("get argument runnumber:" + text);
            context.SetValue(modirunnumber, text -1);
        }
    }

同时,在流程界面设置中,需要将上述变量的赋值活动定义出来。形如下图:

如果是传递给整个流程的初始参数,比如业务工单ID,则需要使用字典传参。

例如:

 Activity workflow1 = new Activityhot();
 IDictionary<string,Object> idparam = new Dictionary<String,Object>();
 idparam.Add("businessid", "20210429001");
 wfApp = new WorkflowApplication(workflow1,idparam);

三、活动的暂停好唤醒

应用场景主要是在并行处理环节,如果采用异步调用,比如活动中开启一个运算线程,希望线程开始计算时挂起当前活动,计算完后再恢复活动继续流转。

这里就得用到书签(bookmark)

 

更多参考https://docs.microsoft.com/zh-cn/dotnet/framework/windows-workflow-foundation/

 

英文版 Windows Workflow Foundation (WF) is a revolutionary part of the .NET 4 Framework that allows you to orchestrate human and system interactions as a series of workflows that can be easily mapped, analyzed, adjusted, and implemented. As business problems become more complex, the need for workflow-based solutions has never been more evident. WF provides a simple and consistent way to model and implement complex problems. As a developer, you focus on developing the business logic for individual workflow tasks. The runtime handles the execution of those tasks after they have been composed into a workflow. Pro WF: Windows Workflow in .NET 4 provides you with the skills you need to incorporate WF in your applications, using a lively tutorial style with each example illustrated in C#. This book gets you up to speed with WF 4 quickly and comprehensively. Learn about WF 4s new designer, its updated programming paradigm, and the completely new set of activities that can enable and extend your workflows. This book also includes detailed coverage of how to customize your workflows and access them in a variety of ways and situations so you can maximize the advantages of this technology. What youll learn * WF 4 basics * New activities and changes to existing activities in WF 4 * Customizing your workflows * Accessing your workflows in a variety of ways in a variety of situations * Using WF with Web Services and ASP.NET * Integrating WCF and WF Who this book is for This book is for intermediate to advanced .NET developers who need to learn how to use the latest version of Windows Workflow Foundation (WF 4). This book is not a primer on .NET or the C# language. To get the most out of the examples presented in this book, it is necessary to have a good working knowledge of .NET 2.0 or higher. All of the examples are presented in C#. Table of Contents * A Quick Tour of Windows Workflow Foundation * Foundation Overview * Activities * Workflow Hosting * Procedural Flow Control * Collection-Related Activities * Flowchart Modeling Style * Host Communication * Workflow Services * Workflow Services Advanced Topics * Workflow Persistence * Customizing Workflow Persistence * Transactions, Compensation, and Exception Handling * Workflow Tracking * Enhancing the Design Experience * Advanced Custom Activities * Hosting the Workflow Designer * WF 3.x Interop and Migration
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值