程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WF4.0基礎篇 (二十一) WorkflowInvoker

WF4.0基礎篇 (二十一) WorkflowInvoker

編輯:關於.NET

本文例子說明

WorkflowInvoker

類名 System.Activitys.WorkflowInvoker 文件 System.Activities.dll 結構說明 繼承 Object

是一個 sealed類

override 了 [CacheMetadata方法] 與 [Execute方法] 功能說明 WorkflowApplication與WorkflowInvoker在調用流程上的區別

在WF4.0中,可以使用WorkflowApplication與 WorkflowInvoker創建並啟動實例.

但兩者在宿主中的線程方式是不一樣的

使用WorkflowInvoker啟動工作流時, 工作流將附加到宿主的線程中執行

使用WorkflowApplication啟動工作流,工作流實例將在宿主線程之外的另一個線程中運行 ,使用的是線程池方式

具體見[Delay 與WF4中的線程]

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());

// invoker.Extensions.Add(myTracking);

// invoker.Invoke();  具體見[Tracking 跟蹤]

本文例子下載:http://files.cnblogs.com/foundation/WorkflowInvokerSample.rar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved