DCSIMG
Bookmark with Timeout - Manu Cohen-Yashar's Blog

Manu Cohen-Yashar's Blog

Bookmark with Timeout

Bookmark is a simple mechanism for asynchronous triggering provided by Workflow 4.0. In a NativeActivity we create a bookmark and wait.  In the host (WorkflowApplication) we resume the bookmark and continue execution. I think that the mechanism is too simple. For example: What about timeouts?

Well it is impossible to set a timeout and limit the time the workflow will wait on the bookmark.

I found a solution by creation a custom activity which wraps the activity that owns the bookmark and use a Pick activity to implement the timeout.

Here is the code:

public sealed class ActivityWithBookMark : NativeActivity

{

   public string BookmarkName { get; set; }

 

   protected override bool CanInduceIdle

   {

      get

      {

          return true;

      }

   }

 

   protected override void Execute(NativeActivityContext context)

   {

      context.CreateBookmark(BookmarkName, Callback);

   }

 

   private void Callback(NativeActivityContext context, Bookmark bookmark,
                         object value)

   {

            // do work ...

        }      

   }

 

 

public sealed class BookmarkWrapper : NativeActivity

{

   private Pick pick = new Pick();

   private PickBranch branch1, branch2;

  

   public TimeSpan Timeout { get; set; }

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

   public string BookmarkName { get; set; }

 

   protected override void CacheMetadata(NativeActivityMetadata metadata)

   {          

     branch1 = new PickBranch();

     branch2 = new PickBranch();

     metadata.AddImplementationChild(pick);

 

     branch1.Trigger = new ActivityWithBookMark()
                           { BookmarkName = BookmarkName };

     branch2.Trigger = new Delay() { Duration = Timeout };

     pick.Branches.Add(branch1);

     pick.Branches.Add(branch2);

 

    base.CacheMetadata(metadata);

}

 

 protected override void Execute(NativeActivityContext context)

{

      context.ScheduleActivity(pick);          

}

}

Hope this helps

Manu

 

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: