locked
Passing parameters to context.ScheduleFunc RRS feed

  • Question

  • I am experimenting with ActivityDelegates. Why does the line in bold print 0 instead of 1. Thanks.

     

    using System;
    using System.Activities;


    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                var wf = new IsTrue
                {
                    Number = 1,
                    Condition = new ActivityFunc<int, bool> { Handler = new CheckOne() }
                };

                WorkflowInvoker.Invoke(wf);
            }

            public class CheckOne : CodeActivity<bool>
            {
                public InArgument<int> Number { get; set; }

                protected override bool Execute(CodeActivityContext context)
                {
                    Console.WriteLine(this.Number.Get(context)); //says 0. Expect 1.

                    var result = Number.Get(context) % 2 == 0;

                    return result;
                }
            }

            public class IsTrue : NativeActivity<bool>
            {
               public InArgument<int> Number { get; set;}

               public ActivityFunc<int, bool> Condition { get; set;}

               protected override void Execute(NativeActivityContext context)
               {
                   this.Result.Set(context, true);

                   Console.WriteLine(this.Number.Get(context)); //says 1

                   context.ScheduleFunc(Condition, this.Number.Get(context), OnCompletion);
               }

                public void OnCompletion(NativeActivityContext context, ActivityInstance completedInstance, bool result)
                {
                    this.Result.Set(context, this.Result.Get(context) && result);
                }
            }

        }
    }

    Thursday, July 1, 2010 6:10 AM

Answers

  • Hi Josh,

    You didn't pass argument to CheckOne, so it won't be able to get it. Please see fixed code here:

     

    using System;
    using System.Activities;
    
    
    namespace ConsoleApplication2
    {
     class Program
     {
      static void Main(string[] args)
      {
       DelegateInArgument<int> arg = new DelegateInArgument<int>();     //define a argument
       var wf = new IsTrue
       {
        Number = 1,
        Condition = new ActivityFunc<int, bool> { Handler = new CheckOne() { Number = arg }, Argument = arg } //pass the arg to Number
       };
    
       WorkflowInvoker.Invoke(wf);
      }
    
      public class CheckOne : CodeActivity<bool>
      {
       public InArgument<int> Number { get; set; }
    
       protected override bool Execute(CodeActivityContext context)
       {
        Console.WriteLine(this.Number.Get(context)); //says 1 now
    
        var result = Number.Get(context) % 2 == 0;
    
        return result;
       }
      }
    
      public class IsTrue : NativeActivity<bool>
      {
       public InArgument<int> Number { get; set; }
    
       public ActivityFunc<int, bool> Condition { get; set; }
    
       protected override void Execute(NativeActivityContext context)
       {
        this.Result.Set(context, true);
    
        Console.WriteLine(this.Number.Get(context)); //says 1
    
        context.ScheduleFunc(Condition, this.Number.Get(context), OnCompletion);
       }
    
       public void OnCompletion(NativeActivityContext context, ActivityInstance completedInstance, bool result)
       {
        this.Result.Set(context, this.Result.Get(context) && result);
       }
      }
    
     }
    }

     


    This posting is provided "AS IS" and confers no rights or warranties.
    Thursday, July 1, 2010 10:19 AM