Open YohDeadfall opened 8 months ago
Methods are made static since while the Dispatcher type isn't static, there's only one instance of it without exceptions.
That’s not true, we have exceptions - XPF ;) And in general, we still want to keep a possibility to allow multiple dispatcher threads, at least for testing.
Then should it be also more WPF compatible, so parameterless Yiled
will use DispatcherPriority.Background
?
Design
The proposal is about adding adding two methods to be used while writing async code for easy thread or priority switching when it's required:
Methods are made static since while the
Dispatcher
type isn't static, there's only one instance of it without exceptions.DispatcherPriorityAwaiter
is a struct, so it can be inlined into a state machine generated by the compiler for the method where the awaiter is used.Usage
The same thing is already provided by WPF via the same named methods. So for newcomers it should take less code to change during migration.
Alternatives
An alternative solution is to use
AwaitWithPriority
, but it requires a task to be passed in which means more allocations and more code to be written.