Dispatcher.Invoke API Change in v11 #9329
-
This code breaks in v11. Dispatcher.Invoke can no longer have return values? Why was this change made, and what is a work-around?
My use-case: I want to display a message box on the UI thread even if called from a background thread, and return the selection. |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments
-
The |
Beta Was this translation helpful? Give feedback.
-
This alone must be the reason why MvvmDialogs breaks with v11. AH got it. It's in the Dispatcher, but missing from IDispatcher! |
Beta Was this translation helpful? Give feedback.
-
If you want to workaround var tcs = new TaskCompletionSource<T>();
_ = dispatcher.InvokeAsync(() =>
{
// ...
tcs.SetResult(...);
}, ...);
var result = await tcs.Task; |
Beta Was this translation helpful? Give feedback.
-
I think it was removed from IDispatcher during the general removal of generic virtual methods from the codebase. Those are known for bad perf especially in AOT scenarios |
Beta Was this translation helpful? Give feedback.
-
Note, that there is always only one dispatcher, I think IDispatcher is only used for our internal unit tests. You can safely use Dispatcher.UIThread, we have no plans for multiple UI thread support in the next 1-2 years. |
Beta Was this translation helpful? Give feedback.
-
Can you explain? |
Beta Was this translation helpful? Give feedback.
-
@mysteryx93 it was done in this PR #7980 Generic virtual methods tend to slow down JIT and blob AOT compiled applications. In some cases it's better to replace that with "object" or make it non-virtual so compiled would have an easier job. |
Beta Was this translation helpful? Give feedback.
-
I think it's okay to have some generic virtual methods in interfaces for convenience (especially for |
Beta Was this translation helpful? Give feedback.
-
We'll need a way to ban the usage of certain methods from our own code then. |
Beta Was this translation helpful? Give feedback.
-
It's really needed for testing purpose; but then our code is also based on IDispatcher instead of Dispatcher and calls that generic virtual method which should be avoided... There's also @hez2010 's work-around. |
Beta Was this translation helpful? Give feedback.
-
Converted this into Q&A as this is neither a bug nor a feature request. If you disagree please let us know. |
Beta Was this translation helpful? Give feedback.
-
We also use |
Beta Was this translation helpful? Give feedback.
If you want to workaround
InvokeAsync<T>
forIDispatcher
, you can useTaskCompletionSource<T>
: