程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WF 4.0基礎篇(十七) Bookmark

WF 4.0基礎篇(十七) Bookmark

編輯:關於.NET

本文例子說明

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

例:基本使用

流程

宿主

//===================================================
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("沒有創ä建實例");
}
}

結果

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

例: 基本使用

流程

宿主

//===================================================
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("沒有創建實例");
}
}

結果

例:在parallel中使用Bookmark

流程

宿主

//===================================================
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("沒有創建實例");
}
}

結果

例:在ParallelForEach中使用Bookmark

流程

參數

 

變量

流程

宿主

//===================================================
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("沒有創建實例");
}
}

結果

例:在ForEach中使用Bookmark

將[在ParallelForEach中使用Bookmark]中的ParallelForEach 換成ForEach

結果

本文 例子下載:http://files.cnblogs.com/foundation/BookmarkSample.rar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved