Windows Workflow – Using the ListenActivity

When the workflow requires interaction with the outside world this activity can become very helpful.

The ListenActivity as its name suggests listens for incoming wake-up calls on the workflow. This activity idles the workflow, and waits for something to happen on one of its branches.

When this activity is dropped into the workflow designer two branches are automatically created (child activities), and these two are the minimum number of branches required to be defined.

On each branch we can only have EventDriven activities, because they are used to handle events; typically they can be raised from the host or by the runtime in response to a delay timer expiring.

Resume:

This activity is mostly used on scenarios where we want to wait for more than one event at the same time, or we want to set a timeout while waiting for some event to get raised.

Each branch is waiting for some event to occur before it can continue with the other activities on the branch. The first occurring event will be the one to run its branch activities first. All other branches cancel and stop listening for events. Basically, the ListenActivity blocks till one branch completes.

Example:

This example is based on a simple Sequential Workflow Console Application. As you can see in the following image, there are two branches on the Listen activity:

  • The one on the left has a HandleExternalEvent activity (for more information about this activity please click here) that is waiting for an external event to be raised (In this case for user to write its name on the console), and below is a Code activity that will display the user name and the current user number.
  • The right branch has a Delay activity that in this case will wait 15 seconds before raising a timeout event. If no external events are raised during the 15 seconds period, the right branch will be executed and a timeout message will be written.

The following image shows an output where the following occurs:

  • A first event is raised for a user named Lucas and the Code activity (Display_UserInput) on the left branch was executed;
  • A second event is raised for a user named Pipas and the Code activity on the left branch was executed again;
  • A timeout occurs. No events were raised for a period of 15 seconds and the Code activity (Display_Timeout_Msg) on the right branch was executed;
  • A third event is raised for a user named Miguel and the left branch activities were executed.

Note:

The ListenActivity cannot be used in state machine workflows.

Just another Blog IT Sites site