1.首先,項目的目標框架要選擇.Net4.5版本,纔有狀態機工做流。sql
2.新建「活動」項,先從工具欄拖出一個StateMachine,而後添加其餘狀態,經過調整狀態間的連線,使兩狀態成爲上一狀態和下一狀態的關係,每種狀態都要有一個終態。數據庫
下面會有代碼介紹如何新建一個工做流。app
1.定位到 C:\Windows\Microsoft.NET\Framework\v4.0.30319\SQL\en 文件夾,須要用到 SqlWorkflowInstanceStoreSchema.sql 和 SqlWorkflowInstanceStoreLogic.sql 這兩個sql文件。框架
2.打開須要加入工做流持久化的數據庫,右擊該數據庫,選擇新建查詢,前後執行兩個腳本,則看到數據庫中多了幾張表(以下圖)ide
3.建立一個空閒時自動實例化的工做流工具
1 //若是你想要使用工做流進行序列化和持久化。
2 WorkflowApplication application = new WorkflowApplication(new TestActivity()); 3
4
5
6 SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(conStr); 7 //讓當前的咱們的applicaton實例跟 數據庫關聯一塊
8 application.InstanceStore = store; 9
10 //當工做流空閒的時候當即讓咱們工做流進行卸載,以前先序列化到 我們的 數據庫裏面去。
11
12 application.PersistableIdle = arg => { return PersistableIdleAction.Unload; }; 13 //當工做流被卸載時顯示卸載信息
14 application.Unloaded += a => { Console.WriteLine("工做流停下來了"); }; 15 application.Run();//啓動一個新的線程幫助咱們執行工做流。
1.新建一個winform項目,更改項目輸出類型爲「控制檯」。ui
2.新建「代碼活動」項,修改新建項的代碼以下:this
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Activities; 6
7 namespace TestWF 8 { 9 //書籤有4步 10 //1.改基類:CodeActivity改成NativeActivity 11 //2.重寫屬性CanInduceIdle,改成return true; 12 //3.修改Execute方法的傳入參數類型CodeActivityContext改成NativeActivityContext 13 //4.利用context.CreateBookmark(text,new BookmarkCallback(MyCallBack));建立書籤
14 public sealed class BookMarkActivity : NativeActivity 15 { 16 // 定義一個字符串類型的活動輸入參數
17 public InArgument<string> Text { get; set; } 18
19 protected override bool CanInduceIdle 20 { 21 get
22 { 23 return true; 24 } 25 } 26
27
28 // 若是活動返回值,則從 CodeActivity<TResult> 29 // 派生並從 Execute 方法返回該值。
30 protected override void Execute(NativeActivityContext context) 31 { 32 // 獲取 Text 輸入參數的運行時值 33 //string text = context.GetValue(this.Text);
34
35 context.CreateBookmark("TestWF",MyCallBack); 36 } 37
38 private void MyCallBack(NativeActivityContext context, Bookmark bookmark, object value) 39 { 40 //能夠在書籤中操做書籤的返回值
41 } 42 } 43 }
3.新建「活動」項,拖出工做流以下圖所示:spa
在審查金額狀態中,添加以下項(若是工具欄中不存在BookMark項,請從新生成解決方案試試):線程
4.在項目中添加app.config配置文件,添加鏈接字符串的appSetting項:
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <startup>
4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5 </startup>
6 <appSettings>
7 <add key="conStr" value="Data Source=./;Initial Catalog=MyOA;Integrated Security=true; "/>
8 </appSettings>
9 </configuration>
5.Form1窗口的設計:
6.在Form1.cs中添加代碼:
1 using System; 2 using System.Activities; 3 using System.Activities.DurableInstancing; 4 using System.Collections.Generic; 5 using System.ComponentModel; 6 using System.Configuration; 7 using System.Data; 8 using System.Drawing; 9 using System.Linq; 10 using System.Text; 11 using System.Threading.Tasks; 12 using System.Windows.Forms; 13
14 namespace TestWF 15 { 16
17 public partial class Form1 : Form 18 { 19 public Form1() 20 { 21 InitializeComponent(); 22 textBox1.Text = Clipboard.GetText(); 23 } 24
25 public static string conStr = ConfigurationManager.AppSettings["conStr"]; 26 private void button3_Click(object sender, EventArgs e) 27 { 28 #region 開啓工做流代碼
29 //若是你想要使用工做流進行序列化和持久化。
30 WorkflowApplication application = new WorkflowApplication(new TestActivity()); 31
32
33
34 SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(conStr); 35 //讓當前的咱們的applicaton實例跟 數據庫關聯一塊
36 application.InstanceStore = store; 37
38 //當工做流空閒的時候當即讓咱們工做流進行卸載,以前先序列化到 我們的 數據庫裏面去。
39
40 application.PersistableIdle = arg => { return PersistableIdleAction.Unload; }; 41 //當工做流被卸載時顯示卸載信息
42 application.Unloaded += a => { Console.WriteLine("工做流停下來了"); }; 43 application.Run();//啓動一個新的線程幫助咱們執行工做流。
44 #endregion
45 textBox1.Text = application.Id.ToString(); 46 //將工做流的guid粘貼到剪貼板中,以便下一次程序啓動時能取到
47 Clipboard.SetText(textBox1.Text); 48
49 } 50
51 private void button2_Click(object sender, EventArgs e) 52 { 53 #region 恢復工做流代碼
54 WorkflowApplication application = new WorkflowApplication(new TestActivity()); 55
56 SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(conStr); 57 //讓當前的咱們的applicaton實例跟 數據庫關聯一塊
58 application.InstanceStore = store; 59
60 //當工做流空閒的時候當即讓咱們工做流進行卸載,以前先序列化到 我們的 數據庫裏面去。
61 application.PersistableIdle = arg => { return PersistableIdleAction.Unload; }; 62
63 application.Load(Guid.Parse(textBox1.Text)); 64 application.ResumeBookmark("TestWF",""); 65 #endregion
66 } 67
68
69 } 70 }
7.運行結果以下圖:
當點擊「開啓工做流」時,提示「請輸入金額」,當輸入金額回車後因爲有書籤,工做流停下來被實例化到數據庫中程序打印出「工做流停下來了」,再點擊「從新開啓工做流」,工做流繼續運行到其餘FinalState狀態。