Wf4: Synchronous execution Workflow

Source: Internet
Author: User

1. In wf4, when we use workflowapplication to execute a workflow, the workflow will be executed asynchronously. We can simply write an example to prove that the workflow settings are as follows:

The host is as follows:

Workflowapplication wfapp = new workflowapplication (New workflow1 ());

Console. writeline ("START Workflow ");
Wfapp. Run ();
Console. writeline ("workflow execution ends ");

The execution result is as follows:

 

2. each workflow instance creates a new thread for asynchronous execution. Previously, we usually used semaphores to control the synchronization effect, which I wrote in many previous articles, however, this is not really a different execution. In wf4, we can set the synchronizationcontext attribute of workflowapplication to execute the workflow synchronously. First, we need to add a class, as shown below:

Class synchronoussynchronizationcontext: synchronizationcontext
{
Public override void post (sendorpostcallback D, object state)
{D (State );}
}

We have rewritten the POST method of synchronizationcontext. If you decompile the POST method of this base class, you can see that the post implemented by the thread pool is a new thread execution initiated from the thread pool, as follows:

Public Virtual void post (sendorpostcallback D, object state)

{

Threadpool. queueuserworkitem (New waitcallback (D), State );

}

We only need to add a line of code in the workflow settings:

Wfapp. synchronizationcontext = new synchronoussynchronizationcontext ();

The results are different, as shown below:

3. Let's take a look at the workflow of long running. First, define an activity as follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Activities;

Namespace carywfapptest1
{

Public sealed class longrunningactivity: nativeactivity
{
Protected override bool caninduceidle
{
Get {return true ;}
}
Protected override void execute (nativeactivitycontext context)
{
Console. writeline ("execute the longrunning logic and wait for completion ...");
Context. createbookmark ("activated", completedcallback );
}

Private void completedcallback (nativeactivitycontext context, Bookmark bookmark, object value)
{
Console. writeline ("longrunning logic execution completed ");
}
}
}

The workflow is designed as follows:

The host code is as follows:

Workflowapplication wfapp = new workflowapplication (New workflow1 ());
Wfapp. synchronizationcontext = new synchronoussynchronizationcontext ();

Wfapp. Idle = E => console. writeline ("triggering workflow idle events ");
Console. writeline ("START Workflow ");
Wfapp. Run ();
Console. writeline ("workflow enters idle state ");
Console. writeline ();
Console. writeline ("start to restore Workflow ");
Console. writeline ("Restore workflow: {0}", wfapp. resumebookmark ("activated", null ));
Console. writeline ("workflow execution ends ");

Console. Readline ();

The execution result is as follows:

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.