Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core - Async functions are blocking UI

maybe I am wrong and this is actually ok behaviour, but then I dont get, why they would even put the async postfix to the method name and make it awaitable.

Just to easily reproduce, this is enough:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    using var ctx = new eWMSContext();
    var a = await ctx.TJobLines.ToListAsync();
}

This call blocks UI even though it seems it shouldn't and wrapping it to Task.Run doesn't seem logical.

I was unable to find any resource why is it happening.

This resource explains, that it should not block UI, but it does: https://learn.microsoft.com/lt-lt/ef/core/miscellaneous/async

I'm using (all are latest for .NET Core 3.1):
EF Core 3.1.12
Oracle.EntityFrameworkCore 3.19.80
Oracle.ManagedDataAccess.Core 2.19.101

like image 287
Tomáš Filip Avatar asked Feb 01 '26 02:02

Tomáš Filip


1 Answers

Unfortunately, the actual implementation of these methods seem to leave something to be desired:

Why would an EF query with ToListAsync hang in a WPF application?

As a workaround, to keep your UI responsive, you could execute the synchronous version on a background thread:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    await Task.Run(() =>
    {
        using var ctx = new eWMSContext()
        {
            var a = ctx.TJobLines.ToList();
        }
    });
}

Then you don't have to rely on the implementation of ToListAsync being non-blocking.

like image 122
mm8 Avatar answered Feb 03 '26 14:02

mm8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!