After completing this chapter, you will learn:
1. Initialize a workflow instance with no parameters and two ways with parameters
2. Determine the status of the workflow instance in your operation
3. Stop Workflow Instance
4. Determine the reason for your workflow to be idle or terminated
A workflow instance consists of one or more activities. (We will begin with the introduction of activities in the seventh chapter: "Basic activity Operations." "Primary activity" or "root activity" is called "Workflow definition". The usual behavior of "workflow definition" is to act as a container for other activities that will work.
Note: "Workflow definition" is what you ask workflow to execute, and a workflow instance is an executing "workflow definition". There is a distinct difference between them, one is being executed, and the other is not.
Where do workflow instances come from? Of course they should be created by you. If you have difficulties to complete this task, and automatically created workflow to meet your application requirements, can also be completed by the software, but at least you have to write workflow tasks or workflow runtime will perform the tasks for you. Microsoft provides the workflow runtime and you have to create the rest of the stuff. After all, this is your application.
WF can help you with the creation of these places, WF will not only execute the workflow instance you created, but will also help you to create them. WF integrates a rich graphical interface designer to help you integrate workflow into the ASP.net Web forms, Windows forms, or WPF applications you create in the same way. You can scroll the mouse over the toolbox, select one from a number of active items, and then drag it to the design interface and release it. If the activity item has configurable properties, you can also configure it with the Properties panel in Visual Studio to make it conform to your intent. We've used the workflow designer briefly in the first chapter, where we'll use it again, after all, the work with WF is almost entirely about creating workflow tasks, and the use of workflow visual designers is a huge part of the development process.
The workflow instance is similar to any other software. They start executing, running, and terminating until a termination condition is encountered. These may be all the records in the database have been processed, all the files that need to be compressed have been compressed, or the documents workflow to each approver have been approved (agreed or disagree), or the process has been completed. It has only one normal startup location, but one or more normal locations that may stop.
Instances can also maintain errors and exceptions. You can handle these exceptions and not deal with them. In some cases, you may not want to deal with the exception that occurred and leave it for later processing.
Sometimes, a workflow process can take a long time to complete. For example, a process sends an order for a part and waits for an order to be received. The type and number of parts must be confirmed before the workflow terminates, which may take days, weeks or even months. So is it necessary for a workflow instance to remain active in memory for days, weeks, or months? What if the server crashes or power outages? Is your workflow instance, data, and application state not all lost?
The activity of workflow instance and constituent instance is an important part of workflow process. WF has provided strong support for the creation and execution of workflow instances. Let's take a look at the WorkflowInstance object.
WorkflowInstance Object Introduction
WorkflowInstance is a WF object that provides you with a separate Workflow task context (environment). You can use this object to find out how things will work in your task. Just as we have methods and attributes to control the workflow runtime, we also have methods and properties that interact with our workflow instances. Table 3-1 lists most of the WorkflowInstance properties, and table 3-2 lists the methods that are frequently used. We'll also see some additional properties and methods in chapter fifth, "Workflow tracking." Table 3-1 WorkflowInstance Properties
Property |
Function |
Instanceid |
Gets the unique identifier of the workflow instance (a GUID) |
WorkflowRuntime |
Get the WorkflowRuntime of this workflow instance |
The method of table 3-2 WorkflowInstance
Method |
Function |
Applyworkflowchanges |
Make changes to the workflow instance by WorkflowChanges object request. This allows you to modify it (add, remove, or change activities) while the workflow is executing, and the workflow instance is paused when the dynamic change is implemented. |
Getworkflowdefinition |
Retrieves the root (root) activity of this workflow instance. |
Resume |
Resumes execution of a workflow instance that was previously paused. If the workflow instance is not in a paused state, nothing is done. If the workflow instance is in a paused state, the workflow runtime triggers the Workflowresumed event after the workflow instance has just been restored. |
Start |
Initiates execution of a workflow instance that invokes Executeactivity on this workflow instance root activity. If start has an exception, it terminates the workflow instance by calling terminate and attaches the exception-related information as the reason for the termination. |
Suspend |
Synchronization pauses this workflow instance. If the workflow instance is in a paused state, nothing is done. If the workflow instance is running, the instance is paused workflow the runtime, then the Suspendorterminateinfoproperty (explain why) and enter suspend to trigger the Workflowsuspended event. |
Terminate |
Terminates the workflow instance synchronously. When the host needs to terminate the workflow instance, the workflow terminates the instance at runtime and attempts to persist the final state of the instance. Then WorkflowInstance set Suspendorterminateinfoproperty (explain why) and enter terminate. Finally, it triggers the workflowterminated event and uploads the termination reason to the message attribute in Workflowterminateexception and is included in the WorkflowTerminatedEventArgs event argument. In addition, if an exception occurs during persistence, the workflow runtime replaces the exception to the WorkflowTerminatedEventArgs event argument. |
There are more workflowinstance related methods that are not listed. To chapter sixth "Loading and unloading of instances", we will see more details of them when we persist the workflow into the database.