• Interesting thing with WF4 Activity Scheduling


    firstly i will show you a certain of  recursive program.  pls see blow.

     

    undoubtedly , the program console will got a message like this ,and along with a System.StackOverflowException .

     

    but let 's see a kind of like this code in WF4 which use scheduling activity.

    public class InfiniteLoop : NativeActivity

    {

        public Activity Body { getset; }

     

        protected override void Execute(NativeActivityContext context)

        {

            context.ScheduleActivity(Body, OnBodyCompleted); //*

        }

     

        void OnBodyCompleted(NativeActivityContext context, ActivityInstance inst)

        {

            context.ScheduleActivity(Body, OnBodyCompleted);

        }

    } 

    note : the OnBodyCompleted will be Called back when the body (activity) is finished excuting.

    This is the simplest possible way to schedule an activity. (Almost. Even simpler if we don’t provide a CompletionCallback.) What does this do? Loops forever. Simple enough.

     

    the Caller program is like this.

     

    Question: Why doesn’t this code stack overflow?

    Actually , the program will typing "go" text forever. but why? 

    Answer: ScheduleActivity just schedules an activity. The scheduled activity (and completion callback) don’t actually execute until the current function (e.g. Execute() ) returns. 

    see?  ScheduleActivity just schedules an activity. not excute . so stackoverflow will not happen forever.

    let go deeper into WF4 inside code . for example the IF and While Activity.

    public sealed class If : NativeActivity

    {

        // …

        public Activity Else { getset; }

        public Activity Then { getset; }

        public InArgument<bool> Condition { getset; }

    }

    public sealed class While : NativeActivity

    {

        // …

        public Activity Body { getset; }

        public Activity<bool> Condition { getset; }

        public Collection<Variable> Variables { get; }

    } 

     

     

     

    Here’s something a little interesting. Both an If activity and a While activity have a Conditionproperty. But they are not implemented the same way. Why? The answer is scheduling control. In the implementation of the While activity, it is necessary to control exactly when Condition is being evaluated in relation to the while loop’s Body. It does something like this:

     

    in Execute():

     

    context.ScheduleActivity<bool>(Condition, OnConditonComplete);

     

    in OnConditionComplete():

     

    context.ScheduleActivity(Body, OnBodyComplete);

     

    What we learn here is how to easily schedule or evaluate workflow expressions:ScheduleActivity<bool> is a function that allows an easy way of getting the Result value of an expression. The result will be passed in to the CompletionCallback<bool> function OnCoditionComplete. 

    the snippet of While Activity code is blow.

     

    public class TwoChildrenNativeActivity

    {

        public Activity FirstChild { getset; }

        public Activity SecondChild { getset; }

     

        protected override void Execute(NativeActivityContext context)

        {

            context.ScheduleActivity(FirstChild, OnChildCompleted); //*

            context.ScheduleActivity(SecondChild, OnChildCompleted); //*

        }

     

        void OnChildCompleted(NativeActivityContext context, ActivityInstance inst); //print which child completed

    }

     

    Question: What does this code do?

     

    Answer: Argh! Not what it first looked like to me! It’s going to depend a lot on what FirstChild and SecondChild are.

     

    Point 1 – scheduling children for execution is LIFO. Last child scheduled is first child in line for the chop (execution)! With a simple example you will probably see this output:

     

    *Child2Completed*

    *Child1Completed*

     

    Point 2 – these children run in parallel. Even though SecondChild will begin executing first, it is possible that FirstChild will complete before SecondChild. A trace would might something like this:

     

    *Child2Started*

    *BookmarkCreatedInChild2*

    *Child2GoesIdle*

    *Child1Started*

    *Child1Completed*
    *BookmarkResumedInChild2*

    *Child2Completed*

     

    Point 3 – in Point 2 parallel doesn’t mean multithreaded. It just means unspecified execution order.

     

    个人认为作者要说明的意思是,原则上。是遵从FILO,但是还是要看这两个Activity的具体情况。执行的结果可能都大不相同,就象上面三种情况。 

    Question: What if I really wanted those two activities to execute in Sequence? FirstChild then SecondChild.

     

    Answer: Don’t schedule SecondChild for execution until FirstChild has completed. 

    如有问题,欢迎指正,谢谢。 

  • 相关阅读:
    POJ3481(待完善版本,请看注释)
    Heap_Sort金老师模板
    poj2255(Tree_Recover)
    快慢指针
    Link_List
    Perl_Tkx_Canvas绘图功能函数介绍
    配置管理
    变更管理
    合同管理
    收尾存在的问题
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2551910.html
Copyright © 2020-2023  润新知