public void Given_An_ICommand_When_Calling_RunCommandInBackground_Should_call_Completed_in_MainThread() { // THIS TEST CREATES, SHOWS AND TEARS DOWN A FORM FOR TESTING - MESSAGE PUMP NEEDED!!! int completedThreadId = -1; var command = new ThreadAwareCommand { CompletedHandler = state => { completedThreadId = Thread.CurrentThread.ManagedThreadId; } }; var form = new CrossThreadingTestForm(command); Application.Run(form); Assert.IsTrue(Thread.CurrentThread.ManagedThreadId != command.ExecutingThreadId, "Command was executed on the same thread that created it!"); Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == completedThreadId, "Completed was not called on the mainthread!"); }
/// <summary> /// Initializes a new instance of the <see cref="CrossThreadingTestForm"/> class. /// </summary> public CrossThreadingTestForm(ThreadAwareCommand command) { InitializeComponent(); CheckForIllegalCrossThreadCalls = true; Values = new List<int>{1, 2}; this.target.DataSource = Values; Mock<IEventAggregator> aggregator = new Mock<IEventAggregator>(); CommandManager manager = new CommandManager(aggregator.Object, SynchronizationContext.Current); manager.Do( Execute.The(command) .RunCommandInBackground()); }
public void Given_An_ICommand_When_executing_without_Undo_but_with_BackgroundThread_Should_create_a_new_Thread() { ManualResetEvent resetEvent = new ManualResetEvent(false); Mock <IEventAggregator> aggregator = new Mock <IEventAggregator>(); CommandManager manager = new CommandManager(aggregator.Object, SynchronizationContext.Current); var command = new ThreadAwareCommand(); manager.Do( Execute.The(command) .WithoutUndoRedo() .RunCommandInBackground() .AndWhenCompletedCall(state => resetEvent.Set())); resetEvent.WaitOne(1000, false); Assert.IsTrue(command.ExecutingThreadId != Thread.CurrentThread.ManagedThreadId, "ThreadIds must be different!"); }