本文例子說明
WorkflowInvoker
類名 System.Activitys.WorkflowInvoker 文件 System.Activities.dll 結構說明 繼承 Object
WorkflowInvoker靜態方式
public static TResult Invoke<TResult>(Activity<TResult> workflow);
public static IDictionary<string, object> Invoke(Activity workflow);
public static TResult Invoke<TResult>(Activity<TResult> workflow, IDictionary<string, object> inputs);
public static IDictionary<string, object> Invoke(Activity workflow, IDictionary<string, object> inputs);
public static IDictionary<string, object> Invoke(Activity workflow, TimeSpan timeout);
public static TResult Invoke<TResult>(Activity<TResult> workflow, IDictionary<string, object> inputs, TimeSpan timeout);
public static IDictionary<string, object> Invoke(Activity workflow, IDictionary<string, object> inputs, TimeSpan timeout);
public static TResult Invoke<TResult>(Activity<TResult> workflow, IDictionary<string, object> inputs, out IDictionary<string, object> additionalOutputs, TimeSpan timeout);
例:得到有返回值Activity的返回值
流程
public sealed class addActivity :NativeActivity<int>
{
public InArgument<int> X { get; set; }
public InArgument<int> Y { get; set; }
protected override void Execute(NativeActivityContext context)
{
int x = X.Get(context);
int y = Y.Get(context);
context.SetValue(base.Result, x + y);
}
}
調用
static void staticCallActivityResult()
{
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 1 }, { "Y", 2 } };
int result = WorkflowInvoker.Invoke(new addActivity(), p);
System.Console.WriteLine(result);
}
結果
例:得到OutArgument
流程
調用
static void staticCallOutArgument()
{
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 3 }, { "Y", 4 } };
System.Collections.Generic.IDictionary<string, object> outArgument = WorkflowInvoker.Invoke(new testWorkflow (), p);
foreach (var item in outArgument)
{
System.Console.WriteLine(item);
}
}
結果
例:得到OutArgument與有返回值Activity的返回值
流程
public sealed class resultAndOutArgumentActivity : NativeActivity<int>
{
public InArgument<int> X { get; set; }
public InArgument<int> Y { get; set; }
public OutArgument<int> myValue { set; get; }
protected override void Execute(NativeActivityContext context)
{
int x = X.Get(context);
int y = Y.Get(context);
context.SetValue(base.Result, x + y);
context.SetValue(myValue, x + y);
}
}
調用
static void staticCallResultAndOutArgument()
{
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 5 }, { "Y", 6 } };
System.Collections.Generic.IDictionary<string, object> outArgument;
int result = WorkflowInvoker.Invoke(new resultAndOutArgumentActivity(), p,out outArgument,new TimeSpan (0));
System.Console.WriteLine(result);
foreach (var item in outArgument)
{
System.Console.WriteLine(item);
}
}
結果
WorkflowInvoker實例方式
WorkflowInvoker實例方式調用工作流時,有返回值Activity的返回值與OutArgument都由 System.Collections.Generic.Dictionary<string, object>集合返回
例:得到有返回值Activity的返回值
流程
public sealed class addActivity :NativeActivity<int>
{
public InArgument<int> X { get; set; }
public InArgument<int> Y { get; set; }
protected override void Execute(NativeActivityContext context)
{
int x = X.Get(context);
int y = Y.Get(context);
context.SetValue(base.Result, x + y);
}
}
調用
static void callActivityResult()
{
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 7 }, { "Y", 8 } };
WorkflowInvoker invoker = new WorkflowInvoker(new addActivity());
System.Collections.Generic.IDictionary<string, object> result = invoker.Invoke(p);
foreach (var item in result)
{
System.Console.WriteLine(item);
}
}
結果
例:得到OutArgument
流程
調用
static void callOutArgument()
{
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 9 }, { "Y", 10 } };
WorkflowInvoker invoker = new WorkflowInvoker(new testWorkflow());
System.Collections.Generic.IDictionary<string, object> outArgument = invoker.Invoke(p);
foreach (var item in outArgument)
{
System.Console.WriteLine(item);
}
}
結果
例:得到OutArgument與有返回值Activity的返回值
流程
public sealed class resultAndOutArgumentActivity : NativeActivity<int>
{
public InArgument<int> X { get; set; }
public InArgument<int> Y { get; set; }
public OutArgument<int> myValue { set; get; }
protected override void Execute(NativeActivityContext context)
{
int x = X.Get(context);
int y = Y.Get(context);
context.SetValue(base.Result, x + y);
context.SetValue(myValue, x + y);
}
}
調用
static void callResultAndOutArgument()
{
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 11 }, { "Y", 12 } };
WorkflowInvoker invoker = new WorkflowInvoker(new resultAndOutArgumentActivity());
System.Collections.Generic.IDictionary<string, object> outArgument;
outArgument = invoker.Invoke(p);
foreach (var item in outArgument)
{
System.Console.WriteLine(item);
}
}
結果
例:InvokeAsync方法與invokeCompleted事件
流程
public sealed class resultAndOutArgumentActivity : NativeActivity<int>
{
public InArgument<int> X { get; set; }
public InArgument<int> Y { get; set; }
public OutArgument<int> myValue { set; get; }
protected override void Execute(NativeActivityContext context)
{
int x = X.Get(context);
int y = Y.Get(context);
context.SetValue(base.Result, x + y);
context.SetValue(myValue, x + y);
}
}
調用
#region //async
static void async()
{
WorkflowInvoker myInvoker = new WorkflowInvoker(new resultAndOutArgumentActivity());
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 13 }, { "Y", 14 } };
myInvoker.InvokeCompleted += new EventHandler<InvokeCompletedEventArgs>(myInvoker_InvokeCompleted);
myInvoker.InvokeAsync(p);
}
static void myInvoker_InvokeCompleted(object sender, InvokeCompletedEventArgs e)
{
foreach (var item in e.Outputs)
{
System.Console.WriteLine(item);
}
}
#endregion
結果
例:BeginInvoke方法與EndInvoke方法
流程
public sealed class resultAndOutArgumentActivity : NativeActivity<int>
{
public InArgument<int> X { get; set; }
public InArgument<int> Y { get; set; }
public OutArgument<int> myValue { set; get; }
protected override void Execute(NativeActivityContext context)
{
int x = X.Get(context);
int y = Y.Get(context);
context.SetValue(base.Result, x + y);
context.SetValue(myValue, x + y);
}
}
調用
#region //begion end
static void beginEnd()
{
WorkflowInvoker myInvoker = new WorkflowInvoker(new resultAndOutArgumentActivity());
var p = new System.Collections.Generic.Dictionary<string, object> { { "X", 15 }, { "Y", 16 } };
IAsyncResult ir = myInvoker.BeginInvoke(p, new AsyncCallback(callback), myInvoker);
System.Collections.Generic.IDictionary<string, object> outArgument = myInvoker.EndInvoke(ir);
foreach (var item in outArgument)
{
System.Console.WriteLine(item);
}
}
static void callback(IAsyncResult asyncResult)
{
System.Threading.ManualResetEvent mr = asyncResult.AsyncWaitHandle as System.Threading.ManualResetEvent;
System.Console.WriteLine("callback");
}
#endregion
結果
例:使用跟蹤
// WorkflowInvoker invoker = new WorkflowInvoker(new testWorkflow());本文例子下載:http://files.cnblogs.com/foundation/WorkflowInvokerSample.rar