private bool OnStage1Complete(IAsyncResult lastResult, WorkflowApplication application, bool isStillSync)
            {
                if (lastResult != null)
                {
                    application.Controller.EndFlushTrackingRecords(lastResult);
                }

                IAsyncResult result = null;

                if (application.RaiseIdleEvent())
                {
                    if (application.Controller.IsPersistable && application.persistenceManager != null)
                    {
                        var persistableIdleHandler = application.PersistableIdle;

                        if (persistableIdleHandler != null)
                        {
                            var action = PersistableIdleAction.None;

                            application.handlerThreadId = Thread.CurrentThread.ManagedThreadId;

                            try
                            {
                                application.isInHandler = true;
                                action = persistableIdleHandler(new WorkflowApplicationIdleEventArgs(application));
                            }
                            finally
                            {
                                application.isInHandler = false;
                            }

                            if (TD.WorkflowApplicationPersistableIdleIsEnabled())
                            {
                                TD.WorkflowApplicationPersistableIdle(application.Id.ToString(), action.ToString());
                            }

                            if (action != PersistableIdleAction.None)
                            {
                                var operation = PersistenceOperation.Unload;

                                if (action == PersistableIdleAction.Persist)
                                {
                                    operation = PersistenceOperation.Save;
                                }
                                else if (action != PersistableIdleAction.Unload)
                                {
                                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidIdleAction));
                                }

                                application.EventData.NextCallback = this.Stage2Callback;
                                result = application.BeginInternalPersist(operation, ActivityDefaults.InternalSaveTimeout, true, EventFrameCallback, application.EventData);

                                if (!result.CompletedSynchronously)
                                {
                                    return(false);
                                }
                            }
                        }
                        else
                        {
                            // Trace the default action
                            if (TD.WorkflowApplicationPersistableIdleIsEnabled())
                            {
                                TD.WorkflowApplicationPersistableIdle(application.Id.ToString(), PersistableIdleAction.None.ToString());
                            }
                        }
                    }
                }

                return(this.OnStage2Complete(result, application, isStillSync));
            }
Beispiel #2
0
            /// <summary>
            /// Called when [stage1 complete].
            /// </summary>
            /// <param name="lastResult">The last result.</param>
            /// <param name="instance">The instance.</param>
            /// <param name="isStillSync">if set to <c>true</c> [is still synchronize].</param>
            /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
            private bool OnStage1Complete(IAsyncResult?lastResult, WorkflowApplication instance, bool isStillSync)
            {
                if (lastResult != null)
                {
                    instance.Controller.EndFlushTrackingRecords(lastResult);
                }
                var completionState = instance.Controller.GetCompletionState(out var outputs, out var completionException);

                if (instance.invokeCompletedCallback == null)
                {
                    var handler = instance.Completed;

                    if (handler != null)
                    {
                        instance.handlerThreadId = Thread.CurrentThread.ManagedThreadId;

                        try
                        {
                            instance.isInHandler = true;
                            handler(new WorkflowApplicationCompletedEventArgs(instance, completionException, completionState, outputs));
                        }
                        finally
                        {
                            instance.isInHandler = false;
                        }
                    }
                }

                switch (completionState)
                {
                case ActivityInstanceState.Closed:
                    if (TD.WorkflowApplicationCompletedIsEnabled())
                    {
                        TD.WorkflowApplicationCompleted(instance.Id.ToString());
                    }
                    break;

                case ActivityInstanceState.Canceled:
                    if (TD.WorkflowInstanceCanceledIsEnabled())
                    {
                        TD.WorkflowInstanceCanceled(instance.Id.ToString());
                    }
                    break;

                case ActivityInstanceState.Faulted:
                    if (TD.WorkflowApplicationTerminatedIsEnabled())
                    {
                        TD.WorkflowApplicationTerminated(instance.Id.ToString(), completionException);
                    }
                    break;
                }

                IAsyncResult?result = null;

                Fx.Assert(instance.Controller.IsPersistable, "Should not be in a No Persist Zone once the instance is complete.");
                if (instance.persistenceManager != null || instance.HasPersistenceModule)
                {
                    instance.EventData.NextCallback = this.Stage2Callback;
                    result = instance.BeginInternalPersist(PersistenceOperation.Unload, ActivityDefaults.InternalSaveTimeout, true, EventFrameCallback, instance.EventData);

                    if (!result.CompletedSynchronously)
                    {
                        return(false);
                    }
                }
                else
                {
                    instance.MarkUnloaded();
                }

                return(this.OnStage2Complete(result, instance, isStillSync));
            }