Workflow queue is used in the hostProgramOr send messages between the external service of the workflow and the activity in the workflow.WFThe description in this article is vague. The following example can be used to understand the use of workflow queues.
The main function of this example isReadlineAt the activity, wait for the user's input in the console. If the user does not enter the content, the process will be deactivated, and there will be a bookmark (in fact, the event processing method) added to the workflow queue.QueueitemavailableEvent.
Next, let's take a lookReadlineFor more information about this activity, see annotations.
Using system;
Using system. workflow. componentmodel;
Using system. workflow. runtime;
Namespace essentialwf. Activities {
Public class Readline: Activity {
Private string text;
Public String text {
Get {return this. Text ;}
}
Protected override void initialize (iserviceprovider provider ){
//When the activity is initialized, it is created with the activity name. You don't have to worry about the duplicate names between multiple instances, because the message queue follows the process instance.
Workflowqueuingservice qservice = (workflowqueuingservice) provider. getservice (typeof (workflowqueuingservice ));
If (! Qservice. exists (this. Name ))
Qservice. createworkflowqueue (this. Name, true );
}
Protected override activityexecutionstatus execute (activityexecutioncontext context ){
Workflowqueuingservice qservice = context. getservice <workflowqueuingservice> ();
//Try to get user input. If user input exists,DequeueMethod To obtain
//And set the activity execution status to end.
Workflowqueue queue = qservice. getworkflowqueue (name );
If (queue. Count> 0 ){
This. Text = (string) queue. dequeue ();
Return activityexecutionstatus. closed;
}
//The attempt failed.QueueitemavailableEvent (this event will be triggered when the message is sent to the queue) plus Processing Method
//And set the activity execution status to running
Queue. queueitemavailable + = This. continueat;
Return activityexecutionstatus. Executing;
}
//I have understood it well below. I will not talk about it anymore.
Void continueat (Object sender, queueeventargs e ){
Activityexecutioncontext context = sender as activityexecutioncontext;
Workflowqueuingservice qservice = context. getservice <workflowqueuingservice> ();
Workflowqueue queue = qservice. getworkflowqueue (name );
This. Text = (string) queue. dequeue ();
Context. closeactivity ();
}
Protected override void uninitialize (iserviceprovider provider ){
Workflowqueuingservice qservice = (workflowqueuingservice) provider. getservice (typeof (workflowqueuingservice ));
If (qservice. exists (this. Name ))
Qservice. deleteworkflowqueue (this. Name );
}
}
}
Let's look at how our host ProgramReadlineActivities.
String S = console. Readline ();
instance. enqueueitem ("rl1", S, null, null); // Code in this line adds a message to the queue. rl1 is the name of the activity, S is the data information of the message. The last two parameters are used to receive feedback notifications after passing the message.