WF4 - 调用NativeActivityContext.ScheduleDelegate的正确方法是什么?

时间:2011-04-24 15:18:08

标签: c# c#-4.0 workflow-foundation workflow-foundation-4

public class NativeActivity1 : NativeActivity
{
    public NativeActivity1()
    {
        var myDynamicActivity = ActivityXamlServices.Load(@"C:\WorkflowConsoleApplication1\WorkflowConsoleApplication1\Workflow1.xaml") as DynamicActivity;
        var argInt32 = new InOutArgument<int>();

        this.ChildActivity = new DynamicActivity
        {
            Properties = { new DynamicActivityProperty() { Name="argInt32", Type=typeof(InOutArgument<int>), Value=argInt32 }, },
            Implementation = () => new Sequence
            {
                Activities =
                {
                    myDynamicActivity,
                    new WriteLine { Text = new InArgument<string>(ctx => argInt32.Get(ctx).ToString()) }
                }
            }
        };
    }
    public DynamicActivity ChildActivity { get; set; }
    protected override void Execute(NativeActivityContext context)
    {
        var parameter = 10;
        while (0 < parameter--)
        {
            var activityInstance = context.ScheduleDelegate(
                new ActivityAction { Handler = this.ChildActivity }
                , new Dictionary<string, object> { { "argInt32", parameter } }
                , (activityContext, completedInstance, outArguments) =>
                {
                    Console.WriteLine("Output:" + outArguments["argInt32"].ToString());
                }, (faultContext, propagatedException, propagatedFrom) =>
                {
                    Console.WriteLine("Fault");
                });
        }
    }
}

    class Program
    {
        static void Main(string[] args)
        {
            WorkflowInvoker.Invoke(new NativeActivity1());
            Console.WriteLine("Press any key to end the process ...");
            Console.ReadLine();
        }
    }

我有一个while循环,它使迭代从1到10.每个增量数都传递给工作流,工作流假设通过乘以-1返回负值。因为我必须留在Execute方法并执行迭代,我认为使用参数调用工作流的唯一方法是使用NativeActivityContext.ScheduleDelegate。程序中唯一的限制是不使用WorkflowInvoker.Invoke。有人知道如何使用ScheduleDelegate吗?

谢谢,莫伊兹

1 个答案:

答案 0 :(得分:1)

我写了一个可能对你有所帮助的样本。见WF4 How To Invoke a Child Workflow as XAML