public void TestInvokeStaticMethodAsyncInSequence() { var t1 = new Variable <int>("t1"); var a = new InvokeMethod <int>() { MethodName = "GetSomething", TargetType = this.GetType(), RunAsynchronously = true, Result = t1, }; var s = new System.Activities.Statements.Sequence() { Variables = { t1 }, Activities = { new Plus() { X = 2, Y = 3 }, a, new Multiply() { X = 3, Y = 7 }, }, }; var r = WorkflowInvoker.Invoke(s); System.Diagnostics.Debug.WriteLine("Something invoke"); //So all run in sequences. The async activity is not being executed in fire and forget style, but probably just good not freezing the UI thread if UI is involved. }
public void TestWorkflowApplicationTwice() { AutoResetEvent syncEvent = new AutoResetEvent(false); var a = new System.Activities.Statements.Sequence() { Activities = { new System.Activities.Statements.Delay() { Duration = TimeSpan.FromSeconds(3), }, new Multiply() { X = 3, Y = 7, } }, }; var app = new WorkflowApplication(a); int mainThreadId = Thread.CurrentThread.ManagedThreadId; int workFlowThreadId = -1; app.Completed = delegate(WorkflowApplicationCompletedEventArgs e) { workFlowThreadId = Thread.CurrentThread.ManagedThreadId; syncEvent.Set(); }; var dt = DateTime.Now; app.Run(); Assert.True((DateTime.Now - dt).TotalSeconds < 2, "app.Run() should not be blocking"); syncEvent.WaitOne(); Assert.NotEqual(mainThreadId, workFlowThreadId); var syncEvent2 = new AutoResetEvent(false); var app2 = new WorkflowApplication(a) { Completed = e => { syncEvent2.Set(); } }; dt = DateTime.Now; app2.Run(); syncEvent2.WaitOne(); var seconds = (DateTime.Now - dt).TotalSeconds; Debug.WriteLine($"Second run takes {seconds} seconds"); }
public void TestWorkflowApplication() { AutoResetEvent syncEvent = new AutoResetEvent(false); 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; int workFlowThreadId = -1; var app = new WorkflowApplication(a) { Completed = delegate(WorkflowApplicationCompletedEventArgs e) { workFlowThreadId = Thread.CurrentThread.ManagedThreadId; syncEvent.Set(); }, }; var dt = DateTime.Now; app.Run(); var seconds = (DateTime.Now - dt).TotalSeconds; System.Diagnostics.Debug.WriteLine($"It takes {seconds} seconds to init a run.");//The 1st WorkflowApplication may take over 1 second to run. Assert.True(seconds < 2, "app.Run() should not be blocking"); syncEvent.WaitOne(); Assert.NotEqual(mainThreadId, workFlowThreadId); }
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); }
public void TestDynamicActivityWithInvokeMethod() { var a = new DynamicActivity { DisplayName = "Dynamic Plus", Properties = { new DynamicActivityProperty() { Name = "ZZ", Type = typeof(OutArgument <int>), } }, Implementation = () => { Variable <int> t1 = new Variable <int>("t1"); var plus = new Plus() { X = new ArgumentValue <int>() { ArgumentName = "XX" }, Y = new ArgumentValue <int>() { ArgumentName = "YY" }, Z = t1, }; var s = new System.Activities.Statements.Sequence() { Variables = { t1 }, Activities = { new InvokeMethod <string>() { MethodName = "DoSomething", TargetObject = new InArgument <InvokeMethodTests>(c => this), Parameters = { new InArgument <string>("Abcd") }, }, new InvokeMethod <int>() { MethodName = "GetSomething", TargetType = this.GetType(), Result = t1, }, new System.Activities.Statements.Assign <int> { To = new ArgumentReference <int>{ ArgumentName = "ZZ" }, //So the Value will be assigned to property ZZ. Noted that ArgumentReference<> is a CodeActivity<> Value = new InArgument <int>(env => t1.Get(env)), //So the Value will be wired from t1 in context. }, }, }; return(s); }, }; var r = WorkflowInvoker.Invoke(a); var threadId = (int)r["ZZ"]; Assert.Equal(System.Threading.Thread.CurrentThread.ManagedThreadId, threadId); }
public void TestDynamicActivityGeneric() { var x = 100; var y = 200; var a = new DynamicActivity <int> { DisplayName = "Dynamic Plus", Properties = { new DynamicActivityProperty() { Name = "XX", Type = typeof(InArgument <int>), }, new DynamicActivityProperty() { Name = "YY", Type = typeof(InArgument <int>), }, }, Implementation = () => { var t1 = new Variable <int>("t1"); var plus = new Plus() { X = new ArgumentValue <int>() { ArgumentName = "XX" }, Y = new ArgumentValue <int>() { ArgumentName = "YY" }, Z = t1, //So Output Z will be assigned to t1 }; var s = new System.Activities.Statements.Sequence() { Variables = { t1 }, Activities = { plus, new System.Activities.Statements.Assign <int> { To = new ArgumentReference <int> { ArgumentName = "Result" }, //I just had a good guess about how Result get assigned. Value = new InArgument <int>(env => t1.Get(env)), }, }, }; return(s); }, }; var dic = new Dictionary <string, object>(); dic.Add("XX", x); dic.Add("YY", y); var r = WorkflowInvoker.Invoke(a, dic); Assert.Equal(300, r); }
public void TestDynamicActivityGenericWithGeneric() { var x = 100; var y = 200; var a = new DynamicActivity <long> { DisplayName = "Dynamic Multiply", Properties = { new DynamicActivityProperty() { Name = "XX", Type = typeof(InArgument <long>), }, new DynamicActivityProperty() { Name = "YY", Type = typeof(InArgument <long>), }, }, Implementation = () => { var t1 = new Variable <long>("t1"); var multiply = new System.Activities.Expressions.Multiply <long, long, long>() { Left = new ArgumentValue <long>() { ArgumentName = "XX" }, Right = new ArgumentValue <long>() { ArgumentName = "YY" }, Result = t1, }; var s = new System.Activities.Statements.Sequence() { Variables = { t1 }, Activities = { multiply, new System.Activities.Statements.Assign <long> { To = new ArgumentReference <long> { ArgumentName = "Result" }, Value = new InArgument <long>(env => t1.Get(env)), }, }, }; return(s); }, }; var dic = new Dictionary <string, object>(); dic.Add("XX", x); dic.Add("YY", y); var r = WorkflowInvoker.Invoke(a, dic); Assert.Equal(20000L, r); }
public void TestDynamicActivity() { var x = 100; var y = 200; var a = new DynamicActivity { DisplayName = "Dynamic Plus", Properties = { new DynamicActivityProperty() { Name = "XX", Type = typeof(InArgument <int>), Value = new InArgument <int>(x), //You can't do Value=x, otherwise, System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Activities.Argument' }, new DynamicActivityProperty() { Name = "YY", Type = typeof(InArgument <int>), //Value=y, }, new DynamicActivityProperty() { Name = "ZZ", Type = typeof(OutArgument <int>), } }, Implementation = () => { Variable <int> t1 = new Variable <int>("t1"); var plus = new Plus() { X = new ArgumentValue <int>() { ArgumentName = "XX" }, Y = new ArgumentValue <int>() { ArgumentName = "YY" }, Z = t1, }; var s = new System.Activities.Statements.Sequence() { Variables = { t1 }, Activities = { plus, new System.Activities.Statements.Assign <int> { To = new ArgumentReference <int> { ArgumentName = "ZZ" }, //So the Value will be assigned to property ZZ. Noted that ArgumentReference<> is a CodeActivity<> Value = new InArgument <int>(env => t1.Get(env)), //So the Value will be wired from t1 in context. }, }, }; return(s); }, }; var dic = new Dictionary <string, object>(); // dic.Add("XX", x); dic.Add("YY", y); var r = WorkflowInvoker.Invoke(a, dic); Assert.Equal(300, (int)r["ZZ"]); }