WF4.0 基础篇 (十七) Bookmark

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

 

本文例子下载:

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

本文例子说明

image.png

image.png


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 { getset; }

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

}

}

 

例:基本使用

流程

image.png

宿主

 

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

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("没有创ä建实例");

}

}

结果

image.png

 

有返回值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 { getset; }

 

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

}

}

 

例:基本使用

流程

image.png

宿主

 

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

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("没有创建实例");

}

}

结果

image.png

 

例:在parallel中使用Bookmark

流程

image.png

宿主

 

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

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("没有创建实例");

}

}

结果

image.png

 

例:在ParallelForEach中使用Bookmark

流程

参数

image.png

 

变量

image.png

 

流程

010810_1310_WF4011.png

宿主

 

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

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<stringobject> dictionary = new Dictionary<stringobject>();

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("没有创建实例");

}

}

结果

image.png

 

例:在ForEach中使用Bookmark

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

image.png

 

结果

image.png

 

 

 


#转载请注明出处 www.skcircle.com 《少有人走的路》勇哥的工业自动化技术网站。如果需要本贴图片源码等资源,请向勇哥索取。
扫码加本站公众号

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«   2021年2月   »
1234567
891011121314
15161718192021
22232425262728
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:

Powered By Z-BlogPHP 1.6.0 Valyria

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864