public void ExecuteWorkflowInstance(WorkflowApplication workflowInstance, Guid rootWorkItemId) { workflowInstance.Extensions.Add(ProgressTrackingParticipant); lock (_sinkRoot) { if (_runningWorkflows.ContainsKey(rootWorkItemId)) { throw new ArgumentException( String.Format("Work item with Id '{0}' is already running.", rootWorkItemId), "rootWorkItemId"); } workflowInstance.BeginRun(WorkflowInstanceEndExecute, workflowInstance); _runningWorkflows.Add(rootWorkItemId, workflowInstance); } }
public void TestWorkflowApplicationBeginRun() { var a = new System.Activities.Statements.Sequence() { Activities = { new System.Activities.Statements.Delay() { Duration = TimeSpan.FromSeconds(3), }, new Multiply() { X = 3, Y = 7, } }, }; int mainThreadId = Thread.CurrentThread.ManagedThreadId; bool completedCalled = false; var app = new WorkflowApplication(a) { Completed = delegate(WorkflowApplicationCompletedEventArgs e) { completedCalled = true; }, }; var dt = DateTime.Now; var asyncResult = app.BeginRun(null, null); var seconds = (DateTime.Now - dt).TotalSeconds; System.Diagnostics.Debug.WriteLine($"It takes {seconds} seconds to init a BeginRun.");//The 1st WorkflowApplication may take over 1 second to run. app.EndRun(asyncResult); Assert.False(completedCalled); }
public void TestWorkflowApplicationBeginRunWithException() { var a = new System.Activities.Statements.Sequence() { Activities = { new ThrowSomething() }, }; int mainThreadId = Thread.CurrentThread.ManagedThreadId; bool completedCalled = false; bool exceptionHandlerCalled = false; var app = new WorkflowApplication(a) { Completed = delegate(WorkflowApplicationCompletedEventArgs e) { completedCalled = true; }, OnUnhandledException = args => { exceptionHandlerCalled = true; return(UnhandledExceptionAction.Terminate); }, }; var dt = DateTime.Now; var asyncResult = app.BeginRun(null, null); var seconds = (DateTime.Now - dt).TotalSeconds; System.Diagnostics.Debug.WriteLine($"It takes {seconds} seconds to init a BeginRun.");//The 1st WorkflowApplication may take over 1 second to run. app.EndRun(asyncResult); Assert.False(completedCalled); Assert.False(exceptionHandlerCalled); }
/// <summary> /// Allows the demonstration of various scenarios using /// WorkflowApplication /// </summary> private static void BasicApplicationTest(TestScenario scenario) { AutoResetEvent waitEvent = new AutoResetEvent(false); WorkflowApplication wfApp = new WorkflowApplication( new HostingDemoWorkflow(), new Dictionary <String, Object> { { "ArgNumberToEcho", 1001 }, }); wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e) { switch (e.CompletionState) { case ActivityInstanceState.Closed: Console.WriteLine("Host: {0} Closed - Thread:{1} - {2}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId, e.Outputs["Result"]); break; case ActivityInstanceState.Canceled: Console.WriteLine("Host: {0} Canceled - Thread:{1}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId); break; case ActivityInstanceState.Executing: Console.WriteLine("Host: {0} Executing - Thread:{1}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId); break; case ActivityInstanceState.Faulted: Console.WriteLine( "Host: {0} Faulted - Thread:{1} - {2}:{3}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId, e.TerminationException.GetType(), e.TerminationException.Message); break; default: break; } waitEvent.Set(); }; wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e) { Console.WriteLine( "Host: {0} OnUnhandledException - Thread:{1} - {2}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId, e.UnhandledException.Message); waitEvent.Set(); return(UnhandledExceptionAction.Cancel); }; wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e) { Console.WriteLine("Host: {0} Aborted - Thread:{1} - {2}:{3}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId, e.Reason.GetType(), e.Reason.Message); waitEvent.Set(); }; wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e) { Console.WriteLine("Host: {0} Idle - Thread:{1}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId); }; wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e) { Console.WriteLine("Host: {0} PersistableIdle - Thread:{1}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId); return(PersistableIdleAction.Unload); }; wfApp.Unloaded = delegate(WorkflowApplicationEventArgs e) { Console.WriteLine("Host: {0} Unloaded - Thread:{1}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId); }; try { Console.WriteLine("Host: About to run {0} - Thread:{1}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId); //determine the demonstration scenario switch (scenario) { case TestScenario.Normal: wfApp.Run(); waitEvent.WaitOne(); break; //case TestScenario.TimeSpan: // wfApp.Run(TimeSpan.FromSeconds(1)); // waitEvent.WaitOne(); // break; case TestScenario.Cancel: wfApp.Run(); //Wait just a bit then cancel the workflow Thread.Sleep(1000); wfApp.Cancel(); waitEvent.WaitOne(); break; case TestScenario.Abort: wfApp.Run(); //Wait just a bit then abort the workflow Thread.Sleep(1000); wfApp.Abort("My aborted reason"); waitEvent.WaitOne(); break; case TestScenario.Terminate: wfApp.Run(); //Wait just a bit then terminate the workflow Thread.Sleep(1000); wfApp.Terminate("My termination reason"); waitEvent.WaitOne(); break; case TestScenario.BeginRun: wfApp.BeginRun(delegate(IAsyncResult ar) { Console.WriteLine( "Host: {0} BeginRunCallback - Thread:{1}", wfApp.Id, System.Threading.Thread.CurrentThread.ManagedThreadId); ((WorkflowApplication)ar.AsyncState).EndRun(ar); }, wfApp); waitEvent.WaitOne(); break; default: break; } } catch (Exception exception) { Console.WriteLine("Host: {0} exception:{1}:{2}", wfApp.Id, exception.GetType(), exception.Message); } }