private bool OnStage1Complete(IAsyncResult lastResult, WorkflowApplication instance, Exception exception, Activity source, string sourceInstanceId) { if (lastResult != null) { instance.Controller.EndFlushTrackingRecords(lastResult); } var handler = instance.OnUnhandledException; var action = UnhandledExceptionAction.Terminate; if (handler != null) { try { instance.isInHandler = true; instance.handlerThreadId = Thread.CurrentThread.ManagedThreadId; action = handler(new WorkflowApplicationUnhandledExceptionEventArgs(instance, exception, source, sourceInstanceId)); } finally { instance.isInHandler = false; } } if (instance.invokeCompletedCallback != null) { action = UnhandledExceptionAction.Terminate; } if (TD.WorkflowApplicationUnhandledExceptionIsEnabled()) { TD.WorkflowApplicationUnhandledException(instance.Id.ToString(), source.GetType().ToString(), source.DisplayName, action.ToString(), exception); } switch (action) { case UnhandledExceptionAction.Abort: instance.AbortInstance(exception, true); break; case UnhandledExceptionAction.Cancel: instance.Controller.ScheduleCancel(); break; case UnhandledExceptionAction.Terminate: instance.TerminateCore(exception); break; default: throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidUnhandledExceptionAction)); } return(true); }