1: using System;
2: using System.Activities;
3: using System.Activities.Hosting;
4: using System.Activities.Persistence;
5: using System.Collections.Generic;
6: using System.Threading;
7: using System.Xml.Linq;
8:
9: namespace WorkflowConsoleApplication1
10: {
11: class IncrementExtension : PersistenceParticipant, IWorkflowInstanceExtension
12: {
13: private static XName PersistencePropertyName = XNamespace.Get("urn:example.com:WorkflowDemo/1.0/Increment").GetName("LastValue");
14:
15: int _Index;
16: private WorkflowInstance _Instance;
17: Bookmark _Bookmark;
18:
19: public void Increment(Bookmark bookmark)
20: {
21: ThreadPool.QueueUserWorkItem(state =>
22: {
23: Thread.Sleep(1000);
24: var value = Interlocked.Increment(ref _Index);
25: _Bookmark = bookmark;
26: ContinueWorkflow(value);
27: }, null);
28: }
29:
30: private void ContinueWorkflow(int value)
31: {
32: _Instance.BeginResumeBookmark(_Bookmark, value, CompleteResume, null);
33: }
34:
35: public void CompleteResume(IAsyncResult ar)
36: {
37: var result = _Instance.EndResumeBookmark(ar);
38: }
39:
40: IEnumerable<object> IWorkflowInstanceExtension.GetAdditionalExtensions()
41: {
42: yield break;
43: }
44:
45: void IWorkflowInstanceExtension.SetInstance(WorkflowInstance instance)
46: {
47: _Instance = instance;
48: }
49:
50: protected override void CollectValues(out IDictionary<XName, object> readWriteValues, out IDictionary<XName, object> writeOnlyValues)
51: {
52: readWriteValues = new Dictionary<XName, object>
53: {
54: {PersistencePropertyName, new KeyValuePair<Bookmark, int>(_Bookmark, _Index)}
55: };
56: writeOnlyValues = null;
57: }
58:
59: protected override void PublishValues(IDictionary<XName, object> readWriteValues)
60: {
61: var keyvalue = (KeyValuePair<Bookmark, int>)readWriteValues[PersistencePropertyName];
62: _Index = (int)keyvalue.Value;
63: _Bookmark = (Bookmark)keyvalue.Key;
64:
65: ContinueWorkflow(_Index);
66: }
67: }
68: }