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: