See the question and my original answer on StackOverflow

The error means you're calling UI technology on a thread that's not the UI thread.

You can do this using a ScopedBatch, with an extension method, something like this:

public static CompositionScopedBatch RunScopedBatch(
    this Compositor compositor,
    Action action,
    Action onCompleted = null,
    CompositionBatchTypes types = CompositionBatchTypes.Animation)
    if (compositor == null)
        throw new ArgumentNullException(nameof(compositor));

    if (action == null)
        throw new ArgumentNullException(nameof(action));

    var batch = compositor.CreateScopedBatch(types);
    if (onCompleted != null)
        // note: if Completed finishes too soon
        // it means there was an exception, problem, etc.
        batch.Completed += (s, e) => onCompleted();

    return batch;