Workflow Notes 2 -- state machine workflow, Workflow workflow

Source: Internet
Author: User

Workflow Notes 2 -- state machine workflow, Workflow workflow
State Machine Workflow

In the previous Workflow Note 1-Workflow introduction, the flowchart Workflow is introduced. Later, Microsoft introduced the state machine Workflow, which is more powerful than the flowchart function.

Create a project StatueWorkflowConsoleApp

A Start Node and a State node are automatically added.

 

 

The running result is as follows:

Pay attention to the execution sequence. Next, expand the workflow.

1. Set the global variable Num

2. Double-click T1 to assign a value to the variable Num.

 

new Random().Next(0,10)

The running result is as follows:

Start Workflow

Previously we created a workflow project to start the workflow as follows.

            Activity workflow1 = new Workflow1();            WorkflowInvoker.Invoke(workflow1);

In work, we usually cannot start a workflow in this way. Because after the process is started, I need to monitor the various states of the process. However, when we start a workflow through Invoke, the status of the workflow cannot be monitored.

We can start a workflow through the WorkflowApplication class. Reference: https://msdn.microsoft.com/zh-cn/library/system.activities.workflowapplication (v = vs.110). aspx

1. Create a Windows form application and a Windows workflowapp

2. Right-click the WindowsWorkFlowApp project, create an activity, and add a state machine.

3. Double-click State1 to add the input parameters.

4. Add another status and end status.

5. Double-click FinalState to add the output

Modify State2

6. Modify the Event code of the "Start Workflow" button as follows:

Private void btnStartWorkFlow_Click (object sender, EventArgs e) {WorkflowApplication app = new WorkflowApplication (new Activity1 (), new Dictionary <string, object> () {"InputName ", "Dr. Zhang"}); app. run ();}

Let's take a look at the constructor of WorkflowApplication.

Constructor

Name

Description

WorkflowApplication (Activity)

Use the specified workflow definition to create a new instance of the WorkflowApplication class.

WorkflowApplication (Activity, javasidictionary <String, jsonobject>)

Create a new instance of the WorkflowApplication class, which uses the specified workflow definition and parameter value.

WorkflowApplication (Activity, incluidictionary <String, jsonobject>, worker WorkflowIdentity)

The WorkflowApplication class of the new instance, which uses the specified workflow definition, parameter value, and definition identifier.

WorkflowApplication (Activity, workflow WorkflowIdentity)

The newly created instance WorkflowApplication uses the specified workflow definition and the class that defines the identity.

7. Because I have created a Windows application, to output console information, we must modify the project output method.

8. Run the project

WorkflowApplication Lifecycle

So how can we monitor the status of a workflow? We can use the specified workflow definition to construct a WorkflowApplication, process the required workflow lifecycle events, and call the workflow by calling Run.

Before calling the Run method, register the lifecycle event and add the following code:

# Region workflow lifecycle event app. unloaded = delegate (WorkflowApplicationEventArgs er) {Console. writeLine ("workflow {0} unload. ", er. instanceId) ;}; app. completed = delegate (WorkflowApplicationCompletedEventArgs er) {Console. writeLine ("workflow {0} completed. ", er. instanceId) ;}; app. aborted = delegate (WorkflowApplicationAbortedEventArgs er) {Console. writeLine ("workflow {0} ended. ", er. instanceId) ;}; app. idle = delegate (WorkflowApplicationIdleEventArgs er) {Console. writeLine ("workflow {0} is idle. ", er. instanceId) ;}; app. persistableIdle = delegate (WorkflowApplicationIdleEventArgs er) {Console. writeLine ("persistence"); return PersistableIdleAction. unload;}; app. onUnhandledException = delegate (WorkflowApplicationUnhandledExceptionEventArgs er) {Console. writeLine ("OnUnhandledException in Workflow {0} \ n {1}", er. instanceId, er. unhandledException. message); return UnhandledExceptionAction. terminate;}; # endregion

Run the project again and the result is as follows:

Multithreading semaphore mechanism

The Run method starts a new thread. In general, we need to wait for the workflow to run completely before returning to the main thread. Then we can useAutoResetEvent,A thread waits for a signal by callingWaitOneUpperAutoResetEvent.

CreateAutoResetEventObject.

AutoResetEvent syncEvent = new AutoResetEvent(false);

Then, in the Completed event of the workflow, execute the Set Method and Set the event status to a signal to allow one or more threads to continue to execute. To put it bluntly, it is a wake-up operation.

syncEvent.Set();

Add

syncEvent.WaitOne();

The WaitOne method will stop the current thread until the current WaitHandle receives a signal to implement the function of waiting for the workflow thread to complete running.

Download source code: WorkflowConsoleApp2.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.