2011-04-24 60 views
0
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调用具有参数的工作流的唯一方法是使用NativeActivityContext.ScheduleDelegate。程序中唯一的限制是不使用WorkflowInvoker.Invoke。有人知道如何使用ScheduleDelegate吗?WF4 - 调用NativeActivityContext.ScheduleDelegate的正确方法是什么?

谢谢,Moiz

+0

不太确定你在这里试图做什么。您正在调用没有参数的DynamicActivity,只是带有输入参数的变量。 – Maurice 2011-04-26 06:52:39

+0

莫里斯,我更新了我的问题。如果你知道答案,请发帖。 – Moiz 2011-04-27 23:17:21

回答

1

我写了一个样本,可能会帮助你。见WF4 How To Invoke a Child Workflow as XAML

+0

感谢Ron的回答,我正在寻找一种方法在代码中做到这一点。我仍然想知道如何使用NativeActivityContext.ScheduleDelegate。在我的工作中,我有一个场景,在调用工作流来构建参数之前,我会做几件事情。因为当我在循环中使用Workflow.Invoker时,我遇到了性能问题,因此我有兴趣查看是否可以保留在运行时间内并调用ScheduleDelegate,以便我不需要在n上调用Invoker。 – Moiz 2011-04-30 14:20:50

相关问题