作者:VB.NET开发
项目:System.Workflow.Runtime.Hostin
' Save the workflow instance state at the point of persistence with option of locking the instance state if it is shared
' across multiple runtimes or multiple phase instance updates
Protected Overrides Sub SaveWorkflowInstanceState(ByVal rootActivity As System.Workflow.ComponentModel.Activity, ByVal unlock As Boolean)
Dim contextGuid As Guid = CType(rootActivity.GetValue(Activity.ActivityContextGuidProperty), Guid)
Console.WriteLine("Saving instance: 0}" + vbLf, contextGuid)
SerializeToFile( _
WorkflowPersistenceService.GetDefaultSerializedForm(rootActivity), contextGuid)
' See when the next timer (Delay activity) for this workflow will expire
Dim timers As TimerEventSubscriptionCollection = CType(rootActivity.GetValue(TimerEventSubscriptionCollection.TimerCollectionProperty), TimerEventSubscriptionCollection)
Dim subscription As TimerEventSubscription = timers.Peek()
If subscription IsNot Nothing Then
' Set a system timer to automatically reload this workflow when it's next timer expires
Dim timeDifference As TimeSpan = subscription.ExpiresAt - DateTime.UtcNow
Dim callback As TimerCallback = New TimerCallback(AddressOf ReloadWorkflow)
Dim timer As New System.Threading.Timer( _
callback, _
subscription.WorkflowInstanceId, _
CType(IIf(timeDifference < TimeSpan.Zero, TimeSpan.Zero, timeDifference), TimeSpan), _
New TimeSpan(-1))
End If
End Sub