diff --git a/src/ReactiveUI.Tests/Commands/ReactiveCommandTest.cs b/src/ReactiveUI.Tests/Commands/ReactiveCommandTest.cs index 9ee43f1316..f3018b3dbe 100644 --- a/src/ReactiveUI.Tests/Commands/ReactiveCommandTest.cs +++ b/src/ReactiveUI.Tests/Commands/ReactiveCommandTest.cs @@ -9,6 +9,7 @@ using System.Reactive.Concurrency; using System.Reactive.Linq; using System.Reactive.Subjects; +using System.Threading; using System.Threading.Tasks; using System.Windows.Input; using DynamicData; @@ -1183,5 +1184,29 @@ public async Task ReactiveCommandCreateFromTaskHandlesTaskExceptionAsync() Assert.False(isExecuting); Assert.Equal("break execution", fail?.Message); } + + [Fact] + public async Task ReactiveCommandExecutesFromInvokeCommand() + { + var semaphore = new SemaphoreSlim(0); + var command = ReactiveCommand.Create(() => semaphore.Release()); + + Observable.Return(Unit.Default) + .InvokeCommand(command); + + var result = 0; + var task = semaphore.WaitAsync(); + if (await Task.WhenAny(Task.Delay(TimeSpan.FromMilliseconds(100)), task).ConfigureAwait(true) == task) + { + result = 1; + } + else + { + result = -1; + } + + await Task.Delay(200).ConfigureAwait(false); + Assert.Equal(1, result); + } } } diff --git a/src/ReactiveUI/ReactiveCommand/ReactiveCommand.cs b/src/ReactiveUI/ReactiveCommand/ReactiveCommand.cs index 0c6875812d..a3cf7f9836 100644 --- a/src/ReactiveUI/ReactiveCommand/ReactiveCommand.cs +++ b/src/ReactiveUI/ReactiveCommand/ReactiveCommand.cs @@ -766,13 +766,14 @@ protected internal ReactiveCommand( .CombineLatest(_isExecuting, (canEx, isEx) => canEx && !isEx) .DistinctUntilChanged() .Replay(1) - .RefCount() - .ObserveOn(_canExecuteScheduler); + .RefCount(); _results = _synchronizedExecutionInfo.Where(x => x.Demarcation == ExecutionDemarcation.Result) .Select(x => x.Result); - _canExecuteSubscription = _canExecute.Subscribe(OnCanExecuteChanged); + _canExecuteSubscription = _canExecute + .ObserveOn(_canExecuteScheduler) + .Subscribe(OnCanExecuteChanged); } private enum ExecutionDemarcation