Methods that are or can be async should switch to the main thread when necessary rather than throw an exception if invoked from a different thread. This allows callers to invoke any async method from any thread without having to concern themselves with the threading requirements of a method that can support its own threading requirements by switching.
async Task FooAsync() {
ThreadHelper.ThrowIfNotOnUIThread();
DoStuff();
await DoMoreStuff();
}
Use await SwitchToMainThreadAsync()
instead:
async Task FooAsync() {
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
DoStuff();
await DoMoreStuff();
}