You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
public class Dispatcher
{
+ public static DispatcherPriorityAwaiter Yield() =>+ new DispatcherPriority.Background);+ public static DispatcherPriorityAwaiter Yield(DispatcherPriority priority)+ new(priority);
}
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
async Task DoSomethingAsync(){// Do something in background// ...// Here we go to UIawait Dispatcher.Yield();// Now it's possible to interact with UI components}
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.
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.The text was updated successfully, but these errors were encountered: