自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解WF 4.0中如何實(shí)現(xiàn)子流程

開(kāi)發(fā) 后端
本文將為大家介紹WF 4.0中,如何在主流程中啟用一些子流程,并通過(guò)簡(jiǎn)單實(shí)例來(lái)詳細(xì)介紹實(shí)現(xiàn)過(guò)程。

在WF 4.0中,存在不同種類(lèi)的流程。本文將介紹的子流程如何在主流程中實(shí)現(xiàn)的原理,希望對(duì)大家有所幫助。

工作流服務(wù)中,經(jīng)常會(huì)在主流程啟用一些子流程。我在審批流程中經(jīng)常會(huì)使用bookmark來(lái)暫停流程,這篇文章,將結(jié)合bookmark來(lái)實(shí)現(xiàn)主流程啟動(dòng)子流程。

使用以前的一篇WF4.0自定義持久化中的自定義的持久化。不過(guò)數(shù)據(jù)表中加入了一個(gè)字段parentid,用于標(biāo)識(shí)父流程:

代碼

下面用一個(gè)流程實(shí)例為例說(shuō)明主流程是如何啟用子流程,子流程又是如何返回主流程的,主流程如下:

主流程

***個(gè)節(jié)點(diǎn)“***站審核”和第三個(gè)節(jié)點(diǎn)“第二站審核”都是BookMark書(shū)簽,附BookMark的代碼如下:

代碼

  1. public sealed class Read<TResult> : NativeActivity<TResult>  
  2.     {  
  3.         public Read()  
  4.             : base()  
  5.         {  
  6.         }  
  7.  
  8.         public string BookmarkName { getset; }  
  9.  
  10.         // Define an activity input argument of type string  
  11.         public InArgument<string> Text { getset; }  
  12. // Must return true for a NativeActivity that creates a bookmark  
  13.         protected override bool CanInduceIdle  
  14.         {  
  15.             get { return true; }  
  16.         }  
  17.  
  18.         protected override void Execute(NativeActivityContext context)  
  19.         {  
  20. context.CreateBookmark(this.BookmarkName, new BookmarkCallback(this.Continue));  
  21.         }  
  22.  void Continue(NativeActivityContext context, Bookmark bookmark, object obj)  
  23.         {  
  24.             this.Result.Set(context, (TResult)obj);  
  25.         } 

第二個(gè)節(jié)點(diǎn)“啟用子流程”,它是一個(gè)自定義的節(jié)點(diǎn),代碼如下:

代碼

  1. public sealed class CallChild : Activity  
  2.    {  
  3.  
  4.        public string FlowName { getset; }  
  5.  
  6.        public CallChild()  
  7.        {  
  8.   base.Implementation = new Func<Activity>(CreateBody);  
  9.        }  
  10.  
  11.        public Activity CreateBody()  
  12.        {  
  13.  
  14.            return new Sequence  
  15.            {  
  16.                DisplayName = "子流程",  
  17.                Activities =  
  18.                    {     
  19.                        new ChildCodeActivity  
  20.                        {  
  21.                            FlowName=this.FlowName        
  22.                        }  
  23.                        ,  
  24.                        new Read<string>  
  25.                        {  
  26.                            BookmarkName="CallChild",  
  27.                           
  28.                        }                   
  29.                    }       };        }    } 

注意上面的ChildCodeActivity類(lèi),實(shí)際上是在ChildCodeActivity中啟動(dòng)子流程的,ChildCodeActivity后面是一個(gè)書(shū)簽,用于暫停主流程。當(dāng)子流程完成后,在子流程中恢復(fù)這個(gè)書(shū)簽,子流程結(jié)束,主流程繼續(xù)往下跑。這個(gè)活動(dòng)中有一個(gè)FlowName屬性,用于表示是啟用哪個(gè)子流程。ChildCodeActivity代碼如下:

代碼

  1. sealed class ChildCodeActivity : CodeActivity  
  2.     {  
  3. // Define an activity input argument of type string  
  4.         public string FlowName { getset; }  
  5. // If your activity returns a value, derive from CodeActivity<TResult>  
  6. // and return the value from the Execute method.  
  7.         protected override void Execute(CodeActivityContext context)  
  8.         {  
  9.             Guid ChildGuid;  
  10.             ChildGuid = WorkFlowRun.CreateAndRun(FlowName);  
  11. InstancesTable obj = InstancesTableBiz.GetInstancesTable(ChildGuid);
  12. //取得子流程的id  
  13.             obj.parentid = context.WorkflowInstanceId;  
  14.             InstancesTableBiz.UpdateInstancesTable(obj);//跟新父流程id  
  15.         }  
  16.     } 

WorkFlowRun.CreateAndRun(FlowName)根據(jù)FlowName啟動(dòng)相應(yīng)的子流程,并得到實(shí)例的Guid。并將子流程的parentid修改改成主流程的guid。

子流程的示例如下:

子流程的示例

#T#

子流程的***個(gè)節(jié)點(diǎn)“子流程***站審核”和第二個(gè)節(jié)點(diǎn)“子流程第二站審核”也都是BookMark書(shū)簽。

***一個(gè)節(jié)點(diǎn)是“結(jié)束”。這個(gè)節(jié)點(diǎn)也至關(guān)重要,因?yàn)槲沂鞘褂眠@個(gè)節(jié)點(diǎn),從子流程中返回到主流程的。因此,每個(gè)子流程都會(huì)有End節(jié)點(diǎn),它的代碼如下:

代碼

  1. public sealed class End : CodeActivity  
  2.   {  
  3.       // Define an activity input argument of type string  
  4.       public InArgument<string> Text { getset; }  
  5.  
  6.  // If your activity returns a value, derive from CodeActivity<TResult>  
  7.       // and return the value from the Execute method.  
  8.       protected override void Execute(CodeActivityContext context)  
  9.       {  
  10.           // Obtain the runtime value of the Text input argument  
  11.           string text = context.GetValue(this.Text);  
  12.           Guid id = context.WorkflowInstanceId;  
  13.           InstancesTable Obj = InstancesTableBiz.GetInstancesTable(id);  
  14.           if (Guid.Empty != Obj.parentid)//如果是子流程,返回父流程  
  15.           {  
  16.   WorkFlowRun.Submit(Obj.parentid, "ParentProcess""returnMain");  
  17.           }  
  18.       }  
  19.   } 

這是我思考出的在WF4.0中一個(gè)啟用子流程的方案,如果你有什么更好的方案和建議,請(qǐng)給我留言,謝謝。

原文標(biāo)題:WF4.0中實(shí)現(xiàn)子流程

鏈接:http://www.cnblogs.com/zhuqil/archive/2010/01/31/SubProcessDemo.html

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2010-01-14 14:12:14

Visual Stud

2009-07-16 10:41:40

WF 4.0 beta

2010-01-14 09:35:10

WF4.0

2009-06-15 10:20:47

WF 4.0 Beta跟蹤機(jī)制

2009-10-28 09:23:27

WF4.0 Beta2

2009-06-17 10:51:58

WF4.0規(guī)則引擎

2009-06-22 09:36:06

WF 4.0 beta跟蹤配置

2009-10-22 08:54:56

WF4 Beta 2

2009-12-04 09:14:05

.NET 4.0

2009-03-23 10:54:12

.NET契約式編程編程思想

2009-10-20 15:03:29

ExpandoObje

2009-05-20 10:26:09

Visual StudWF微軟

2010-01-05 09:26:13

.NET 4.0

2023-11-29 15:53:45

2009-09-27 10:03:53

Silverlight

2009-07-24 10:00:38

.NET 4.0內(nèi)存映

2023-10-04 09:44:56

Btrfs子卷

2011-08-29 14:59:26

QtEvent事件

2009-06-03 14:50:17

C# 4.0泛型協(xié)變性

2011-09-09 19:05:28

Widget
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)