首页 > 编程开发 > Objective-C编程 >
-
WF 4.0 Beta之旅 4 BookMark的使用
WF4.0中加入了书签(Bookmark)机制,可以让活动在书签的位置挂起,等待恢复。如果我们需要在活动没有完成的时候,提供额外的数据,这个时候我
们就可以给活动注册一个书签,我们需要使用CreateNamedBookmark方法创建书签,该方法有三个参数一个名字,一个BookmarkCallback的委托,当
活动恢复的时候执行。和BookmarkOptions,Runtime为每个工作流实例维护一个Bookmarks的Table,每次有Bookmark要恢复的时候都回去check。
1.下面我们就创建一个这样的自定义活动,Read.cs,代码如下:
using System;
using System.Activities;
public sealed class Read<T> : NativeActivity<T>
{
public Read() : base()
{
}
public string BookmarkName { get; set; }
protected override void Execute(ActivityExecutionContext context)
{
context.CreateNamedBookmark(this.BookmarkName, new BookmarkCallback(this.Continue));
}
void Continue(ActivityExecutionContext context, Bookmark bookmark, object obj)
{
this.Result.Set(context, (T)obj);
}
}
2.设计工作流
工作流中包含一个Parallel和一个WriteLine,工作流中的变量如下:
Name:x type:String scope:Sequence
Name:y type:String scope:Sequence
Parallel中有两个Read活动,两个read活动的BookmarkName属性为x,y。Result属性也为x,y
3.宿主程序如下:
namespace Bookmarks
{
using System;
using System.Linq;
using System.Threading;
using System.Activities;
using System.Activities.Statements;
using System.Collections;
class Program
{
static void Main(string[] args)
{
ManualResetEvent completionEvent = new ManualResetEvent(false);
AutoResetEvent idleEvent = new AutoResetEvent(false);
WorkflowInstance instance = new WorkflowInstance(new Sequence1());
instance.OnIdle += delegate
{
idleEvent.Set();
return IdleAction.Nothing;
};
instance.OnCompleted += delegate
{
completionEvent.Set();
};
instance.Run();
bool lastBookmark = false;
while (!lastBookmark)
{
idleEvent.WaitOne();
IList bookmarks = instance.GetAllBookmarks();
if (bookmarks == null || bookmarks.Count == 0)
{
break;
}
lastBookmark = (bookmarks.Count == 1);
while (true)
{
Console.Write("Bookmarks:");
foreach (BookmarkInfo info in bookmarks)
{
Console.Write(" '" + info.BookmarkName + "'");
}
Console.WriteLine();
Console.WriteLine("输入要恢复的书签名");
string name = Console.ReadLine();
Console.WriteLine("输入传入的数据 '{0}'", name);
string str = Console.ReadLine();
try
{
instance.ResumeBookmark(name, str);
break;
}
catch (BookmarkNotFoundException e)
{
Console.WriteLine(e.Message);
}
}
}
completionEvent.WaitOne();
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
}
通过 IList bookmarks = instance.GetAllBookmarks();获得所有书签。
通过instance.ResumeBookmark(name, str);恢复活动的执行。
4.结果如下:
系列文章:
WF4.0 Beta1之旅(1):基本介绍
WF4.0 Beta1之旅(2):异常处理
WF4.0 Beta1之旅(3):全新的FlowChart
Parallel中有两个Read活动,两个read活动的BookmarkName属性为x,y。Result属性也为x,y
3.宿主程序如下:
namespace Bookmarks
{
using System;
using System.Linq;
using System.Threading;
using System.Activities;
using System.Activities.Statements;
using System.Collections;
class Program
{
static void Main(string[] args)
{
ManualResetEvent completionEvent = new ManualResetEvent(false);
AutoResetEvent idleEvent = new AutoResetEvent(false);
WorkflowInstance instance = new WorkflowInstance(new Sequence1());
instance.OnIdle += delegate
{
idleEvent.Set();
return IdleAction.Nothing;
};
instance.OnCompleted += delegate
{
completionEvent.Set();
};
instance.Run();
bool lastBookmark = false;
while (!lastBookmark)
{
idleEvent.WaitOne();
IList bookmarks = instance.GetAllBookmarks();
if (bookmarks == null || bookmarks.Count == 0)
{
break;
}
lastBookmark = (bookmarks.Count == 1);
while (true)
{
Console.Write("Bookmarks:");
foreach (BookmarkInfo info in bookmarks)
{
Console.Write(" '" + info.BookmarkName + "'");
}
Console.WriteLine();
Console.WriteLine("输入要恢复的书签名");
string name = Console.ReadLine();
Console.WriteLine("输入传入的数据 '{0}'", name);
string str = Console.ReadLine();
try
{
instance.ResumeBookmark(name, str);
break;
}
catch (BookmarkNotFoundException e)
{
Console.WriteLine(e.Message);
}
}
}
completionEvent.WaitOne();
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
}
通过 IList bookmarks = instance.GetAllBookmarks();获得所有书签。
通过instance.ResumeBookmark(name, str);恢复活动的执行。
4.结果如下: