-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Feature] Dispatcher.YieldAsync() for UWP #3064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for submitting a new feature request! I've automatically added a vote reaction to help get things started. Other community members can vote to help us prioritize this feature in the future! |
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established. |
This seems like an interesting feature, and nice job with the PR as well, I appreciate the insights that went into building that awaitable type. I just have a few question on the feature as a whole. I'm not entirely sure what's an actual, real-world use case scenario for this. I saw the sample code in this issue and I get how that would work, but I'm confused as to what actual problem users might face would be solved by this new featute. You would typically need to yield somehow (or to run an update on the dispatcher) when you're executing blocking code on the UI thread. But the point is that you really shouldn't be doing that in the first place: if you need to run some CPU-bound stuff you (eg. some processing loop of some kind) you should just offload that to Here's a very simple example, suppose you have this in code-behind somewhere: int[] data = new int[100];
// Instead of this...
for (int i = 0; i < data.Length; i++)
{
ExpensiveProcessItem(data[i]);
ProgressBar.Value = i;
await Dispatcher.YieldAsync();
}
// Shouldn't it be this?
IProgress<int> progress = new Progress<int>(i => ProgressBar.Value = i);
await Task.Run(() =>
{
for (int i = 0; i < data.Length; i++)
{
ExpensiveProcessItem(data[i]);
progress.Report(i);
}
}); Of course, in a real-world scenario you'd likely want to wrap that items processing as a method in your backend and only expose an API taking an Overall, I just feel like this feature could potentially incentivize devs to put more "backend" code in code-behind, using this |
Sometimes we only need a few simple UI updates apart from progress report, such as debounce and status indicator. private async void Button_Clicked(......)
{
Button.IsEnabled = false;
ProgressRing.IsActive = true;
await Dispatcher.YieldAsync();
await DoNetworkRequest();
Button.IsEnabled = true;
ProgressRing.IsActive = false;
} |
@hez2010 How does the sample above benefit? As soon as |
Consider this ViewModel: private ObservableCollection<string> list; // which has been bind to a ListBox <ListBox ItemSource="{x:Bind list, Mode=OneWay}" /> Now you need to insert plenty of entries to the list: for (var i = 0; i < 100000; i++)
{
list.Add($"test{i}");
} which will lead to long-time UI frozen. But with Dispatcher.YieldAsync(): for (var i = 0; i < 100000; i++)
{
list.Add($"test{i}");
if (i % 100 == 0) await Dispatcher.YieldAsync();
} It can make UI response while inserting to the list, and you can operate the part which has been inserted to the list without need of waiting all data to be inserted. It make less use of |
I'm not sure I'm following your example 🤔 If you use the MVVM pattern, that list would be in your viewmodel, and you'd do all operations from there - the Also other than the point above, I wonder how this ties in with the |
@Sergio0694 Actually you can get a Dispatcher from |
Yeah I know that, I never said that retrieving a
Hope that makes sense! |
Thanks. I'll try out the new |
Uh oh!
There was an error while loading. Please reload this page.
Describe the problem this feature would solve
Notify to update UI while executing code on a UI thread without needs of executing code on Dispatcher explicitly. It's useful for updating a progress bar in an event handler.
Describe the solution
Instead of
The text was updated successfully, but these errors were encountered: