Will await Task.Yield() be optimized away?
See original GitHub issueI am trying to do this on winform and maui framework:
private async void button1_Click(object sender, EventArgs e)
{
while(true)
{
await Task.Yield();
}
}
My expectation is that the interface will not become unresponsive, but the result is the opposite of what I thought, the interface is unresponsive anymore.
But my friend has no problem on Xamarin for Android, as expected. I suspect that Yield is optimized away.
Issue Analytics
- State:
- Created a year ago
- Comments:14 (2 by maintainers)
Top Results From Across the Web
Will await Task.Yield() be optimized away? · Issue #8254
I don't think Task.Yield() can be optimized away. For Winform, it is probably because WindowsFormsSynchronizationContext.
Read more >When would I use Task.Yield()?
With await Task.Yield() , you force it to be asynchronous in a way that the subsequent code is still run on the current...
Read more >How Async/Await Really Works in C# - .NET Blog
ReadAsync and then yield return s that Task ; that yielded task is what will be handed off to IterateAsync after it calls...
Read more >Optimize long tasks - web.dev
When the main thread is congested, tasks scheduled with requestIdleCallback() may never get to run. Use async / await to create yield points...
Read more >Awaiting a Task that has no awaitable code : r/dotnet
A method with the async keyword should always contain at least one await , otherwise there is no reason to write async and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
… it seems unlikely that just adding 2000 elements would be sufficient to lock the UI for a large amount of time, but rather that whatever you’re doing to gather the data is taking a large amount of time. If it’s your UI code, you may need to refactor something - but what you may need to change is better handled in general coding forums. If it’s the UI framework, reporting performance problems is appreciated, but we’d need a repro.
You’re right about this. See some of the later entries for examples.
await
ing the results should be sufficient.Progress<T>
that you can post results (possibly batched) to. This would increase the time it takes to completely all the updates, but should keep it otherwise responsive.In either case, your next step is figuring out which more specific part of your code is slow, then asking for more targeted advice on someplace like StackOverflow or the C# discord.
Unless you can provide a specific repro for slow UI in general, there isn’t anything actionable on our end.
I don’t think it is runtime/threading issue, consider @Clockwork-Muse’s suggestion.
Transferring to MAUI for more suggestions on how such UI should be loaded