question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Will await Task.Yield() be optimized away?

See original GitHub issue

I 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:closed
  • Created a year ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Clockwork-Musecommented, Jun 11, 2022

… 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.

If it is placed in another thread, it will cause an exception.

You’re right about this. See some of the later entries for examples.

  • If updating the UI is actually fast, but gathering the data is slow, just awaiting the results should be sufficient.
  • If updating the UI is for some reason slow, you’d probably best be served by creating some form of 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.

0reactions
buyaa-ncommented, Jun 22, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found