Reprinted and rewritten in C # language,ArticleEasy to learn
This example customizes an operation that persists an instance to a disk.
The first part is the implementation of the persistenceservice class, and the second part is the function class for operating the disk.
Implement the persistenceservice class
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Collections. Specialized;
Using system. workflow. runtime;
Using system. workflow. runtime. Hosting;
Using system. workflow. componentmodel;
Using system. Windows. forms;
namespace workflowlibrary1
{< br> public class custom persistence service: workflowpersistenceservice
{< br>/*
when the instance enters the idle status, whether the engine automatically calls unload
is true, when the instance enters the idle status, the engine automatically calls the instance's unload method.
For details, see
*/
private bool unloadonidlevalue = false in the sqlworkflowpersistenceservice class.
///
// by default, the instance in the idle status is not automatically called by the engine.
///
Public custom persistence service ()
{< BR >}
///
// parameters passed in with data, operation Mode value of the current instance: unloadonidlevalue
///
//
Public custom persistence service (bool unloadonidle))
{< br> unloadonidlevalue = unloadonidle;
}
///
// load the completed activity
///
///
//
//
protected override activity loadcompletedcontextactivity (guid scopeid, activity outeractivity)
{< br> system. collections. arraylist A = new system. collections. arraylist ();
A. Add ("instance:" + scopeid. tostring ());
MessageBox. Show ("preparation: Load completed activity: loadcompletedcontextactivity", A. tostring ());
Object OBJ = disk file operation. Load (scopeid, outeractivity );
Return OBJ as activity;
}
/// <Summary>
/// Load the instance
/// </Summary>
/// <Param name = "instanceid"> </param>
/// <Returns> </returns>
Protected override activity loadworkflowinstancestate (guid instanceid)
{
System. Collections. arraylist A = new system. Collections. arraylist ();
A. Add ("instance:" + instanceid. tostring ());
MessageBox. Show ("preparation: loading instance: loadworkflowinstancestate", A. tostring ());
Object OBJ = disk file operation. Load (instanceid, null );
Return OBJ as activity;
}
/// <Summary>
/// Save the completed status
/// </Summary>
/// <Param name = "activity"> </param>
Protected override void savecompletedcontextactivity (activity)
{
Guid contextguid = new GUID ();
Contextguid = (guid) activity. getvalue (activity. activitycontextguidproperty );
System. Collections. arraylist A = new system. Collections. arraylist ();
A. Add ("instance:" + contextguid. tostring ());
Disk File Operation. Save (activity, contextguid, true );
}
/// <Summary>
/// Save the instance
/// </Summary>
/// <Param name = "rootactivity"> </param>
/// <Param name = "unlock"> </param>
Protected override void saveworkflowinstancestate (activity rootactivity, bool unlock)
{
Guid contextguid = new GUID ();
Contextguid = (guid) rootactivity. getvalue (activity. activitycontextguidproperty );
System. Collections. arraylist A = new system. Collections. arraylist ();
A. Add ("instance:" + contextguid. tostring ());
A. Add ("unlock:" + unlock. tostring ());
MessageBox. Show ("preparation: Save conditioning: saveworkflowinstancestate", A. tostring ());
Disk File Operation. Save (rootactivity, contextguid, unlock );
}
/// <Summary>
/// Return the solution mode for the instance in the idle status
/// </Summary>
/// <Param name = "activity"> </param>
/// <Returns> </returns>
Protected override bool unloadonidle (activity)
{
// If it is true, when the instance enters the idle status, the engine automatically calls the instance's unload method,
Guid contextguid = new GUID ();
Contextguid = (guid) activity. getvalue (activity. activitycontextguidproperty );
System. Collections. arraylist A = new system. Collections. arraylist ();
A. Add ("mode Value:" + unloadonidlevalue. tostring ());
A. Add ("instance:" + contextguid. tostring ());
MessageBox. Show ("return the solution mode for the instance in the idle status: unloadonidle", A. tostring ());
Return unloadonidlevalue;
}
/// <Summary>
/// Unlock the locked instance so that the instance can be operated
/// </Summary>
/// <Param name = "rootactivity"> </param>
Protected override void unlockworkflowinstancestate (activity rootactivity)
{
Guid contextguid;
Contextguid = (guid) rootactivity. getvalue (activity. activitycontextguidproperty );
System. Collections. arraylist A = new system. Collections. arraylist ();
A. Add ("unlocked instance:" + contextguid. tostring ());
MessageBox. Show ("preparation: Unlock the locked instance: unlockworkflowinstancestate", A. tostring ());
Disk File Operation. Unlock (contextguid );
}
}
}
Disk operations
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;
Using system. Io. compression;
Using system. Collections. Specialized;
Using system. workflow. runtime;
Using system. workflow. runtime. Hosting;
Using system. workflow. componentmodel;
Using system. Windows. forms;
Namespace workflowlibrary1
{
/// <Summary>
/// This class is an entity operation class that saves workflow instances to disks and loads them from disks.
/// </Summary>
Class Disk File Operations
{
Static filestream stream = NULL;
Static string file name = NULL;
///
// Save the activity instance status to a file
///
/// activity object
// guid of the instance
// lock
Save Public static void (activity rootactivity, guid ID, bool unlock)
{< br> // use the instance id of the current workflow as the file name
file name = ID. tostring ();
Try
{
// If the file exists, delete it.
If (file. exists (File Name ))
File. Delete (File Name );
// Create a stream
Stream = new filestream (file name, filemode. createnew, fileaccess. Write, fileshare. None );
// write a workflow instance to a stream
rootactivity. save (Stream);
}< br> catch
{
}
Finally
{
If (! (Stream = NULL ))
Stream. Close ();
}
MessageBox. Show ("Disk File Operations-save", "saved:" + file name. tostring ());
}
///
///
/// instance ID
//
Public static object loading (guid ID, activity outeractivity)
{< br> file name = ID. tostring ();
Object OBJ = NULL;
Try
{
// Create a stream from the disk
Stream = new filestream (file name, filemode. Open, fileaccess. Read, fileshare. Read );
// Load the stream from the disk
Stream. Seek (0, seekorigin. Begin );
OBJ = activity. Load (stream, outeractivity );
MessageBox. Show ("Disk File Operations-loading", "saved:" + file name. tostring ());
}
Catch
{
}
Finally
{
Stream. Close ();
}
Return OBJ;
}
/// <Summary>
/// This section Code Used to unlock disk files. If the disk files are locked
/// </Summary>
/// <Param name = "ID"> </param>
Public static void unlock (guid ID)
{
Try
{
File name = ID. tostring ();
Stream = new filestream (file name, filemode. Open );
File. setattributes (file name, fileattributes. Normal );
}
Catch
{
}
Finally
{
Stream. Close ();
}
MessageBox. Show ("disk file operation-unlock", "unlock completed:" + file name. tostring ());
}
}
}
Then loadSqlworkflowpersistenceserviceClass.
Workflowruntime engine = new workflowruntime ();
Object custom PS = new workflowlibrary1. custom persistence service (true );
Engine. addservice (custom PS );