/// <summary>
    /// Schedules an action to be executed.
    /// </summary>
    /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
    /// <param name="state">State passed to the action to be executed.</param>
    /// <param name="action">Action to be executed.</param>
    /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
    public override IDisposable Schedule <TState>(TState state, Func <IScheduler, TState, IDisposable> action)
    {
        if (action == null)
        {
            throw new ArgumentNullException(nameof(action));
        }

        if (_dispatcher.CheckAccess())
        {
            return(action(this, state));
        }

        var d = new SingleAssignmentDisposable();

        _context.PostWithStartComplete(() =>
        {
            if (!d.IsDisposed)
            {
                d.Disposable = action(this, state);
            }
        });

        return(d);
    }