コード例 #1
0
        public void RunOnUIThreadDelayedAsync_should_throw_on_null_arguments()
        {
            var         dispatcher = new ThreadDispatcher(_ => Task.CompletedTask);
            Func <Task> func       = async() => await dispatcher.RunOnUIThreadDelayedAsync(null, 0);

            func.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("action");
        }
コード例 #2
0
        public async Task RunOnUIThreadDelayedAsync_should_run_after_a_delay()
        {
            bool ranOnUIThread = false;
            bool ranDelayed    = false;
            bool ranAction     = false;

            var dispatcher = new ThreadDispatcher(
                uiThreadInvoker: action =>
            {
                ranOnUIThread = true;
                action();
                return(Task.CompletedTask);
            },
                delayInvoker: (ms, token) =>
            {
                ranDelayed = true;
                return(Task.CompletedTask);
            });

            await dispatcher.RunOnUIThreadDelayedAsync(() => ranAction = true, 0);

            ranOnUIThread.Should().BeTrue();
            ranDelayed.Should().BeTrue();
            ranAction.Should().BeTrue();
        }
コード例 #3
0
        public async Task RunOnUIThreadDelayedAsync_should_not_run_if_canceled_before_the_delay_timeout()
        {
            var dispatcher         = new ThreadDispatcher(_ => Task.CompletedTask);
            var cancellationSource = new CancellationTokenSource();

            bool actionRan = false;
            Task task      = dispatcher.RunOnUIThreadDelayedAsync(
                () => actionRan = true,
                -1,
                cancellationSource.Token);

            cancellationSource.Cancel();
            await task;

            actionRan.Should().BeFalse();
        }