WF4.0 基础篇 (十七) Bookmark

本节主要介绍WF4中无返回值Bookmark与有返回值Bookmark,以及Bookmark在parallel,ParallelForEach,ForEach中的使用.

 

本文例子下载:

http://files.cnblogs.com/foundation/BookmarkSample.rar

本文例子说明

WF4.0 基础篇 (十七) Bookmark_第1张图片

WF4.0 基础篇 (十七) Bookmark_第2张图片

Bookmark

Bookmark可使用流程Idle并触发实例的OnIdle

 

无返回值Bookmark

要定义具备Bookmark的Activit,可从 NativeActivity继承,override [Execute 方法],使用[NativeActivityContext.CreateBookmark方法]添加Bookmark

需要override [ CanInduceIdle 属性],使其返回值为[True]

可用[实例.ResumeBookmark方法]触发Bookmark

public sealed class noResultBookmark : NativeActivity

{

public InArgument<string> bookmarkName { get; set; }

protected override bool CanInduceIdle

{

get

{ return true; }

}

protected override void Execute(NativeActivityContext context)

{

string bookmark = context.GetValue(bookmarkName);

context.CreateBookmark(bookmark);

System.Console.WriteLine("创建bookmark:{0}", bookmark);

}

}

 

例:基本使用

流程

WF4.0 基础篇 (十七) Bookmark_第3张图片

宿主

 

//===================================================

void workflowCompleted(WorkflowApplicationCompletedEventArgs e)

{

instance = null;

System.Console.WriteLine("workflowCompleted:{0}", e.CompletionState.ToString());

}

 

void aborted(WorkflowApplicationAbortedEventArgs e)

{

instance = null;

System.Console.WriteLine("aborted ,Reason:{0}", e.Reason.Message);

}

 

UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e)

{

System.Console.WriteLine("unhandledException:{0}", e.UnhandledException.Message);

return UnhandledExceptionAction.Cancel;

}

void workflowIdel(WorkflowApplicationIdleEventArgs e)

{

System.Console.WriteLine("Idle:{0}", e.InstanceId);

 

System.Console.WriteLine("--------BookmarkName---------------------------");

foreach (var item in e.Bookmarks)

{

System.Console.WriteLine("{0}", item.BookmarkName);

}

System.Console.WriteLine("================================");

}

//==================================================

 

WorkflowApplication instance = null;

 

private void button_Run_Click(object sender, RoutedEventArgs e)

{

instance = new WorkflowApplication(new WFLibrary.noResultBookmarkWorkflow());

 

instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);

instance.OnUnhandledException = unhandledExceptionl;

instance.Aborted = aborted;

instance.Idle = workflowIdel;

instance.Run();

 

}

 

private void button_triggering_Click(object sender, RoutedEventArgs e)

{

string bookName = textBoxBookmark.Text;

 

if (instance != null)

{

if (instance.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1)

{

instance.ResumeBookmark(bookName, null);

}

else

{

foreach (var v in instance.GetBookmarks())

{

System.Console.WriteLine("--------请从下面选项中选择一BookmarkName---------------------------");

System.Console.WriteLine("BookmarkName:{0}:,OwnerDisplayName:{1}", v.BookmarkName, v.OwnerDisplayName);

System.Console.WriteLine("================================");

}

}

}

else

{

MessageBox.Show("没有创ä建实例");

}

}

结果

WF4.0 基础篇 (十七) Bookmark_第4张图片

 

有返回值Bookmark

要定义具备Bookmark的Activit,可从 NativeActivity<T> 继承,override [Execute 方法],使用[NativeActivityContext.CreateBookmark方法]添加Bookmark

需要override [ CanInduceIdle 属性],使其返回值为[True]

可用[实例.ResumeBookmark方法]触发Bookmark

public sealed class resultBookmark<T> : NativeActivity<T>

{

public InArgument<string> bookmarkName { get; set; }

 

protected override bool CanInduceIdle

{

get

{ return true; }

}

protected override void Execute(NativeActivityContext context)

{

string bookmark = context.GetValue(bookmarkName);

context.CreateBookmark(bookmark,new BookmarkCallback(bookmarkCallback));

System.Console.WriteLine("创建bookmark:{0}", bookmark);

}

void bookmarkCallback(NativeActivityContext context, Bookmark bookmark, object obj)

{

this.Result.Set(context, (T)obj);

}

}

 

例:基本使用

流程

WF4.0 基础篇 (十七) Bookmark_第5张图片

宿主

 

//===================================================

void workflowCompleted(WorkflowApplicationCompletedEventArgs e)

{

instance = null;

System.Console.WriteLine("workflowCompleted:{0}", e.CompletionState.ToString());

}

 

void aborted(WorkflowApplicationAbortedEventArgs e)

{

instance = null;

System.Console.WriteLine("aborted ,Reason:{0}", e.Reason.Message);

}

 

UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e)

{

System.Console.WriteLine("unhandledException:{0}", e.UnhandledException.Message);

return UnhandledExceptionAction.Cancel;

}

void workflowIdel(WorkflowApplicationIdleEventArgs e)

{

System.Console.WriteLine("Idle:{0}", e.InstanceId);

 

System.Console.WriteLine("--------BookmarkName---------------------------");

foreach (var item in e.Bookmarks)

{

System.Console.WriteLine("{0}", item.BookmarkName);

}

System.Console.WriteLine("================================");

}

//==================================================

 

WorkflowApplication instance = null;

 

private void button_Run_Click(object sender, RoutedEventArgs e)

{

instance = new WorkflowApplication(new WFLibrary.resultBookmarkWorkflow());

 

instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);

instance.OnUnhandledException = unhandledExceptionl;

instance.Aborted = aborted;

instance.Idle = workflowIdel;

instance.Run();

 

}

 

private void button_triggering_Click(object sender, RoutedEventArgs e)

{

string bookName = textBoxBookmark.Text;

string inputValue = textBox_Value.Text;

 

if (instance != null)

{

if (instance.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1)

{

instance.ResumeBookmark(bookName,inputValue );

}

else

{

foreach (var v in instance.GetBookmarks())

{

System.Console.WriteLine("--------请从下面选项中选择一BookmarkName---------------------------");

System.Console.WriteLine("BookmarkName:{0}:,OwnerDisplayName:{1}", v.BookmarkName, v.OwnerDisplayName);

System.Console.WriteLine("================================");

}

}

}

else

{

MessageBox.Show("没有创建实例");

}

}

结果

WF4.0 基础篇 (十七) Bookmark_第6张图片

 

例:在parallel中使用Bookmark

流程

WF4.0 基础篇 (十七) Bookmark_第7张图片

宿主

 

//===================================================

void workflowCompleted(WorkflowApplicationCompletedEventArgs e)

{

instance = null;

System.Console.WriteLine("workflowCompleted:{0}", e.CompletionState.ToString());

}

 

void aborted(WorkflowApplicationAbortedEventArgs e)

{

instance = null;

System.Console.WriteLine("aborted ,Reason:{0}", e.Reason.Message);

}

 

UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e)

{

System.Console.WriteLine("unhandledException:{0}", e.UnhandledException.Message);

return UnhandledExceptionAction.Cancel;

}

void workflowIdel(WorkflowApplicationIdleEventArgs e)

{

System.Console.WriteLine("Idle:{0}", e.InstanceId);

 

System.Console.WriteLine("--------BookmarkName---------------------------");

foreach (var item in e.Bookmarks)

{

System.Console.WriteLine("{0}", item.BookmarkName);

}

System.Console.WriteLine("================================");

}

//==================================================

 

WorkflowApplication instance = null;

 

private void button_Run_Click(object sender, RoutedEventArgs e)

{

instance = new WorkflowApplication(new WFLibrary.parallelBookmarkWorkflow());

 

instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);

instance.OnUnhandledException = unhandledExceptionl;

instance.Aborted = aborted;

instance.Idle = workflowIdel;

instance.Run();

 

}

 

private void button_triggering_Click(object sender, RoutedEventArgs e)

{

string bookName = textBoxBookmark.Text;

string inputValue = textBox_Value.Text;

 

if (instance != null)

{

if (instance.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1)

{

instance.ResumeBookmark(bookName,inputValue );

}

else

{

foreach (var v in instance.GetBookmarks())

{

System.Console.WriteLine("--------请从下面选项中选择一BookmarkName---------------------------");

System.Console.WriteLine("BookmarkName:{0}:,OwnerDisplayName:{1}", v.BookmarkName, v.OwnerDisplayName);

System.Console.WriteLine("================================");

}

}

}

else

{

MessageBox.Show("没有创建实例");

}

}

结果

WF4.0 基础篇 (十七) Bookmark_第8张图片

 

例:在ParallelForEach中使用Bookmark

流程

参数

 

变量

WF4.0 基础篇 (十七) Bookmark_第9张图片

 

流程

WF4.0 基础篇 (十七) Bookmark_第10张图片

宿主

 

//===================================================

void workflowCompleted(WorkflowApplicationCompletedEventArgs e)

{

instance = null;

System.Console.WriteLine("workflowCompleted:{0}", e.CompletionState.ToString());

}

 

void aborted(WorkflowApplicationAbortedEventArgs e)

{

instance = null;

System.Console.WriteLine("aborted ,Reason:{0}", e.Reason.Message);

}

 

UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e)

{

System.Console.WriteLine("unhandledException:{0}", e.UnhandledException.Message);

return UnhandledExceptionAction.Cancel;

}

void workflowIdel(WorkflowApplicationIdleEventArgs e)

{

System.Console.WriteLine("Idle:{0}", e.InstanceId);

 

System.Console.WriteLine("--------BookmarkName---------------------------");

foreach (var item in e.Bookmarks)

{

System.Console.WriteLine("{0}", item.BookmarkName);

}

System.Console.WriteLine("================================");

}

//==================================================

 

WorkflowApplication instance = null;

 

private void button_Run_Click(object sender, RoutedEventArgs e)

{

 

System.Collections.Generic.SortedSet<string> nameList = new SortedSet<string>();

nameList.Add("wxd");

nameList.Add("wxwinter");

nameList.Add("lzm");

nameList.Add("dd");

System.Collections.Generic.Dictionary<string, object> dictionary = new Dictionary<string, object>();

dictionary.Add("myNameList", nameList);

 

instance = new WorkflowApplication(new WFLibrary.ParallelForEachWorkflow(), dictionary);

 

instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);

instance.OnUnhandledException = unhandledExceptionl;

instance.Aborted = aborted;

instance.Idle = workflowIdel;

instance.Run();

 

}

 

private void button_triggering_Click(object sender, RoutedEventArgs e)

{

string bookName = textBoxBookmark.Text;

string inputValue = textBox_Value.Text;

 

if (instance != null)

{

if (instance.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1)

{

instance.ResumeBookmark(bookName,inputValue );

}

else

{

foreach (var v in instance.GetBookmarks())

{

System.Console.WriteLine("--------请从下面选项中选择一BookmarkName---------------------------");

System.Console.WriteLine("BookmarkName:{0}:,OwnerDisplayName:{1}", v.BookmarkName, v.OwnerDisplayName);

System.Console.WriteLine("================================");

}

}

}

else

{

MessageBox.Show("没有创建实例");

}

}

结果

WF4.0 基础篇 (十七) Bookmark_第11张图片

 

例:在ForEach中使用Bookmark

将[在ParallelForEach中使用Bookmark]中的ParallelForEach换成ForEach

WF4.0 基础篇 (十七) Bookmark_第12张图片

 

结果

WF4.0 基础篇 (十七) Bookmark_第13张图片

 

 

 

你可能感兴趣的:(bookmark)