Workflow starting and killing another workflow

Once again,  there was an interesting request in the MS Windows Workflow Foundation forum yesterday;
 
I wrote some code this morning to illustrate my solution : read the whole thread (and my code) here :
 
 
 
 

To summarize, I suggest 2 activities + 1 service:

1°an activity for starting a workflow: LaunchWFActivity:

2°a service for killing a workflow: WFShutdownService

3°an activity for killing a workflow: ShutdownWFActivity ; this activity calls the following service ( WFShutdownService )

4°DataBinding ShutdownWFActivity to LaunchWFActivity on the generated Guid

5°in your case using a ListenActivity waiting for the event

 

The following picture illustrates my callerworkflow :

 

 The next picture illustrates my called workflow:
 

   1° an Activity for starting a workflow

—————————————————————————————

        public partial class LaunchWFActivity: Activity
 {
        public static DependencyProperty LaunchedWFGuidProperty = System.Workflow.ComponentModel.DependencyProperty.Register("LaunchedWFGuid", typeof(Guid), typeof(LaunchWFActivity));

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Guid LaunchedWFGuid
        {
            get
            {
                return ((Guid)(base.GetValue(LaunchWFActivity.LaunchedWFGuidProperty)));
            }
            set
            {
                base.SetValue(LaunchWFActivity.LaunchedWFGuidProperty, value);
            }
        }
  public LaunchWFActivity()
  {
   InitializeComponent();
  }
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            IStartWorkflow startWorkflow = executionContext.GetService(typeof(IStartWorkflow)) as IStartWorkflow;
            LaunchedWFGuid = startWorkflow.StartWorkflow(typeof(Workflow2), null);
            return ActivityExecutionStatus.Closed;
        }
 }

 2°A service for killing a workflow : this service will be called by the ShutdownWFActivity (see below)

———————————————————————————————————————  

 class WFShutdownService : WorkflowRuntimeService
    {
        public void ShutDownWorkflow(Guid workflowGuidToShutdown)
        {
            WorkflowInstance wi= this.Runtime.GetWorkflow(workflowGuidToShutdown);
            wi.Terminate("normal shutdown");
            //wi.Abort();
        }
    }

——————————————————————————————————————

3°An Activity for killing a workflow ;

 public partial class ShutdownWFActivity: Activity
 {
        public static DependencyProperty WFToShutDownGuidPropertyProperty = System.Workflow.ComponentModel.DependencyProperty.Register("WFToShutDownGuidProperty", typeof(Guid), typeof(ShutdowWFActivity));

        [Description("This is the description which appears in the Property Browser")]
        [Category("This is the category which will be displayed in the Property Browser")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Guid WFToShutDownGuidProperty
        {
            get
            {
                return ((Guid)(base.GetValue(ShutdowWFActivity.WFToShutDownGuidPropertyProperty)));
            }
            set
            {
                base.SetValue(ShutdowWFActivity.WFToShutDownGuidPropertyProperty, value);
            }
        }
  public ShutdowWFActivity()
  {
   InitializeComponent();
  }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
           
            WFShutdownService myservice =executionContext.GetService<WFShutdownService>();
            myservice.ShutDownWorkflow(this.WFToShutDownGuidProperty);
            return ActivityExecutionStatus.Closed;
        }
 }

4°ShutdownWFActivity::WFToShutDownGuid property can be databound
to a LaunchWFActivity::LaunchedWFGuid property in the designer.

 

5°Now  you can trigger the ShutdownWFActivity after a specific event (in your question you asked for an HandleExternalEventActivity) by using a listenActivity.

 

 
 
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s