Skip to content

Latest commit

 

History

History
117 lines (94 loc) · 6.51 KB

File metadata and controls

117 lines (94 loc) · 6.51 KB
description title ms.date ms.assetid
Learn more about: Bookmarks
Bookmarks - WF
03/30/2017
9b51a346-09ae-455c-a70a-e2264ddeb9e2

Bookmarks

Bookmarks are the mechanism that enables an activity to passively wait for input without holding onto a workflow thread. When an activity signals that it is waiting for stimulus, it can create a bookmark. This indicates to the runtime that the activity’s execution should not be considered complete even when the currently executing method (which created the xref:System.Activities.Bookmark) returns.

Bookmark Basics

A xref:System.Activities.Bookmark represents a point at which execution can be resumed (and through which input can be delivered) within a workflow instance. Typically, a xref:System.Activities.Bookmark is given a name and external (host or extension) code is responsible for resuming the bookmark with relevant data. When a xref:System.Activities.Bookmark is resumed, the workflow runtime schedules the xref:System.Activities.BookmarkCallback delegate that was associated with that xref:System.Activities.Bookmark at the time of its creation.

Bookmark Options

The xref:System.Activities.BookmarkOptions class specifies the type of xref:System.Activities.Bookmark being created. The possible non mutually-exclusive values are xref:System.Activities.BookmarkOptions.None, xref:System.Activities.BookmarkOptions.MultipleResume, and xref:System.Activities.BookmarkOptions.NonBlocking. Use xref:System.Activities.BookmarkOptions.None, the default, when creating a xref:System.Activities.Bookmark that is expected to be resumed exactly once. Use xref:System.Activities.BookmarkOptions.MultipleResume when creating a xref:System.Activities.Bookmark that can be resumed multiple times. Use xref:System.Activities.BookmarkOptions.NonBlocking when creating a xref:System.Activities.Bookmark that might never be resumed. Unlike bookmarks created using the default xref:System.Activities.BookmarkOptions, xref:System.Activities.BookmarkOptions.NonBlocking bookmarks do not prevent an activity from completing.

Bookmark Resumption

Bookmarks can be resumed by code outside of a workflow using one of the xref:System.Activities.WorkflowApplication.ResumeBookmark%2A overloads. In this example, a ReadLine activity is created. When executed, the ReadLine activity creates a xref:System.Activities.Bookmark, registers a callback, and then waits for the xref:System.Activities.Bookmark to be resumed. When it is resumed, the ReadLine activity assigns the data that was passed with the xref:System.Activities.Bookmark to its xref:System.Activities.Activity%601.Result%2A argument.

public sealed class ReadLine : NativeActivity<string>  
{  
    [RequiredArgument]  
    public  InArgument<string> BookmarkName { get; set; }  
  
    protected override void Execute(NativeActivityContext context)  
    {  
        // Create a Bookmark and wait for it to be resumed.  
        context.CreateBookmark(BookmarkName.Get(context),
            new BookmarkCallback(OnResumeBookmark));  
    }  
  
    // NativeActivity derived activities that do asynchronous operations by calling
    // one of the CreateBookmark overloads defined on System.Activities.NativeActivityContext
    // must override the CanInduceIdle property and return true.  
    protected override bool CanInduceIdle  
    {  
        get { return true; }  
    }  
  
    public void OnResumeBookmark(NativeActivityContext context, Bookmark bookmark, object obj)  
    {  
        // When the Bookmark is resumed, assign its value to  
        // the Result argument.  
        Result.Set(context, (string)obj);  
    }  
}  

In this example, a workflow is created that uses the ReadLine activity to gather the user’s name and display it to the console window. The host application performs the actual work of gathering the input and passes it to the workflow by resuming the xref:System.Activities.Bookmark.

Variable<string> name = new Variable<string>  
{  
    Name = "name"  
};  
  
Activity wf = new Sequence  
{  
    Variables =  
    {  
        name  
    },  
    Activities =  
    {  
        new WriteLine()  
        {  
            Text = "What is your name?"  
        },  
        new ReadLine()  
        {  
            BookmarkName = "UserName",  
            Result = name  
        },  
        new WriteLine()  
        {  
            Text = new InArgument<string>((env) => "Hello, " + name.Get(env))  
        }  
    }  
};  
  
AutoResetEvent syncEvent = new AutoResetEvent(false);  
  
// Create the WorkflowApplication using the desired  
// workflow definition.  
WorkflowApplication wfApp = new WorkflowApplication(wf);  
  
// Handle the desired lifecycle events.  
wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)  
{  
    // Signal the host that the workflow is complete.  
    syncEvent.Set();  
};  
  
// Start the workflow.  
wfApp.Run();  
  
// Collect the user's name and resume the bookmark.  
// Bookmark resumption only occurs when the workflow  
// is idle. If a call to ResumeBookmark is made and the workflow  
// is not idle, ResumeBookmark blocks until the workflow becomes  
// idle before resuming the bookmark.  
wfApp.ResumeBookmark("UserName", Console.ReadLine());  
  
// Wait for Completed to arrive and signal that  
// the workflow is complete.  
syncEvent.WaitOne();  

When the ReadLine activity is executed, it creates a xref:System.Activities.Bookmark named UserName and then waits for the bookmark to be resumed. The host collects the desired data and then resumes the xref:System.Activities.Bookmark. The workflow resumes, displays the name, and then completes. Note that no synchronization code is required with regard to resuming the bookmark. A xref:System.Activities.Bookmark can only be resumed when the workflow is idle, and if the workflow is not idle, the call to xref:System.Activities.WorkflowApplication.ResumeBookmark%2A blocks until the workflow becomes idle.

Bookmark Resumption Result

xref:System.Activities.WorkflowApplication.ResumeBookmark%2A returns a xref:System.Activities.BookmarkResumptionResult enumeration value to indicate the results of the bookmark resumption request. The possible return values are xref:System.Activities.BookmarkResumptionResult.Success, xref:System.Activities.BookmarkResumptionResult.NotReady, and xref:System.Activities.BookmarkResumptionResult.NotFound. Hosts and extensions can use this value to determine how to proceed.