Workflow implementation in typical scenarios, such as modemas, mouse run, and host waking

Source: Internet
Author: User

I believe everyone has encountered the use of c # To implement modemy, mouse run, and host waking. This has almost become a typical example of c # events. Today, I plan to use the state machine workflow in wf to implement this scenario. Here the characters are Jerry and Tom in <cat and mouse>. First, let's take a look at some of the effects:

How do you feel? Some people may think that it is a bit difficult to use the state machine workflow to respond to handleExternalEventActivity activities in a classic scenario where the modemas, the mouse run, and the host wakes up? See the following normal process:

The mouse appears in the cave ---> the cat sees the mouse ---> the cat is called ---> the mouse runs ---> the cat catches up with the mouse ---> the host praises the mouse
In this process, what the host needs to respond to is: The Cat sees the mouse, the mouse runs, and the cat catches the mouse. Right? It's a bit blind. Take a look at the state machine workflow diagram:

 

 

 

1. Initialization
This is needless to say. An external method is called to implement mouse appearance.

2. When a cat sees a mouse, wait for the cat to see the mouse first, and then judge whether the cat wants to catch the mouse.

3. When the mouse is running, the host wakes up and hears the call from the mouse. Then, the host responds to the escape event for a period of time and calls an external method to catch the mouse.

4. The mouse catches the cat and waits for the cat to catch the mouse first. According to the situation, the host gives the performance.

5. End

Yes, you should have discovered it. The state here is relatively simple, because there are not many factors to consider. A state machine often has multiple paths for a State. In this case, for example, if a mouse appears in the cave and the cat never sees it, what is it like? If a cat sees a mouse, but it is really cool to catch it, what does it mean? Maybe, when the cat catches the mouse and runs in front of the host, the host will not respond. These all represent different state transitions caused by multiple event triggers. We have time to perfect this scenario.

The basic concepts of workflows and wf cannot be understood and can be linked to this. Http://www.cnblogs.com/sharezone/archive/2009/05/15/1457644.html

Demo download

Communication Interface code:

Public interface ICMMControl
{
// Workflow to host communication
/// <Summary>
/// Host praise
/// </Summary>
Void WorkFlowInit ();
/// <Summary>
/// Call a cat
/// </Summary>
Void CatWow ();
/// <Summary>
/// Catch the cat
/// </Summary>
Void CatCatch ();
/// <Summary>
/// Host praise
/// </Summary>
Void MasterSay ();

// Host to workflow communication
/// <Summary>
/// The cat catches up with the mouse
/// </Summary>
Event EventHandler <CatCatchMouseEventArgs> CatCatchMouse;
/// <Summary>
/// The cat sees the mouse
/// </Summary>
Event EventHandler <CatSeeMouseEventArgs> CatSeeMouse;
/// <Summary>
/// Wake up the mouse
/// </Summary>
Event EventHandler <MouseRunAndMasterWakeArgs> MouseRunAndMasterWake;
}

Communication Protocol implementation code:

 

Public class cmcmconnector: ICMMControl
{
Protected static cmdataservice _ service = null;
Protected static object _ syncLock = new object ();
Public event EventHandler <MouseRunAndMasterWakeArgs> MouseRunAndMasterWake;
Public event EventHandler <CatSeeMouseEventArgs> CatSeeMouse;
Public event EventHandler <CatCatchMouseEventArgs> CatCatchMouse;

Public static cmdataservice
{
Get {return _ service ;}
Set
{
Lock (_ syncLock)
{
_ Service = value;
}
}
}
Public void CatWow ()
{
_ Service. RaiseCatWowEvent ();
}
Public void WorkFlowInit ()
{
_ Service. RaiseWorkFlowInitEvent ();
}
Public void CatCatch ()
{
_ Service. RaiseCatCatchEvent ();
}

Public void MasterSay ()
{
_ Service. RaiseMasterSayEvent ();
}

Public void RaiseCatSeeMouse (Guid instanceID, bool flag)
{
If (CatSeeMouse! = Null)
{
CatSeeMouse (null, new CatSeeMouseEventArgs (instanceID, flag ));
}
}

Public void RaiseCatCatchMouse (Guid instanceID)
{
If (CatCatchMouse! = Null)
{
CatCatchMouse (null, new catchmouseeventargs (instanceID ));
}
}

Public void RaiseMouseRunAndMasterWake (Guid instanceID)
{
If (MouseRunAndMasterWake! = Null)
{
MouseRunAndMasterWake (null, new MouseRunAndMasterWakeArgs (instanceID ));
}
}
}

Communication Service Code:

 

Public class cmdataservice
{
Protected static WorkflowRuntime _ workflowRuntime = null;
Protected static ExternalDataExchangeService _ dataExchangeService = null;
Protected static cmcmconnector _ dataconne= null;
Protected static object _ syncRoot = new object ();

Public event EventHandler CatWow;
Public event EventHandler CatCatch;
Public event EventHandler MasterSay;
Public event EventHandler WorkflowInit;

Private Guid _ instanceID = Guid. Empty;

Public Guid InstanceID
{
Get {return _ instanceID ;}
Set {_ instanceID = value ;}
}

Public static cmdataservice CreateDataService (Guid instanceID, WorkflowRuntime workflowRuntime)
{
Lock (_ syncRoot)
{
If (_ workflowRuntime = null)
{
_ WorkflowRuntime = workflowRuntime;
}

If (_ dataExchangeService = null)
{
_ DataExchangeService = new ExternalDataExchangeService ();
_ WorkflowRuntime. AddService (_ dataExchangeService );
}

CMMS dataConnector = (CMMS ctor) workflowRuntime. GetService (typeof (CMMS connector ));

If (dataConnector = null)
{
_ DataConnector = new cmcmconne ();
_ DataExchangeService. AddService (_ dataConnector );
}
Else
{
_ DataConnector = dataConnector;
}

CMMS dataservice workflowDataService = new CMMS dataservice (instanceID );
CMMS ctor. CMMS dataservice = workflowDataService;

Return workflowDataService;
}
}
Public static cmdataservice GetRegisteredWorkflowDataService ()
{
Lock (_ syncRoot)
{
CMMS dataservice workflowDataService = CMMS ctor. CMMS dataservice;

If (workflowDataService = null)
{
Throw new Exception ("Error processing data service... service cannot be null .");
}

Return workflowDataService;
}
}

Private cmdataservice (Guid instanceID)
{
_ InstanceID = instanceID;
CMMS ctor. CMMS dataservice = this;
}

~ Cmdataservice ()
{
_ WorkflowRuntime = null;
_ DataExchangeService = null;
_ DataConnector = null;
}

Public void RaiseCatWowEvent ()
{
If (_ workflowRuntime = null)
_ WorkflowRuntime = new WorkflowRuntime ();

_ WorkflowRuntime. GetWorkflow (_ instanceID); // loads persisted workflow instances
If (CatWow! = Null)
{
CatWow (this, null );
}
}

Public void RaiseWorkFlowInitEvent ()
{
If (_ workflowRuntime = null)
_ WorkflowRuntime = new WorkflowRuntime ();

_ WorkflowRuntime. GetWorkflow (_ instanceID); // loads persisted workflow instances
If (WorkflowInit! = Null)
{
WorkflowInit (this, null );
}
}

Public void RaiseCatCatchEvent ()
{
If (_ workflowRuntime = null)
_ WorkflowRuntime = new WorkflowRuntime ();

_ WorkflowRuntime. GetWorkflow (_ instanceID );
If (CatCatch! = Null)
{
CatCatch (this, null );
}
}

Public void RaiseMasterSayEvent ()
{
If (_ workflowRuntime = null)
_ WorkflowRuntime = new WorkflowRuntime ();

_ WorkflowRuntime. GetWorkflow (_ instanceID );
If (MasterSay! = Null)
{
MasterSay (this, null );
}
}
}

 

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.