I'm experimenting with async await and I'm encountering UI blocking that shouldn't be happening.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadButton.Click += LoadButton_OnClick;
}
private async void LoadButton_OnClick(object sender, RoutedEventArgs e)
{
LoadButton.IsEnabled = false;
// await Task.Delay(2000);
using(TestContext ctx = new TestContext())
{
IList<User> users = await ctx.Users.ToListAsync();
}
LoadButton.IsEnabled = true;
}
}
If I comment the DbContext bit and uncomment Task.Delay, it behaves as expected - non blocking the UI.
Based on my understanding, the ToListAsync() method is still invoked from the UI thread but shouldn't block it. Even if the method is CPU-bound (probably isn't) it would cause lag, not a complete block.
My questions:
Do I understand this correctly?
Why is my UI blocking while awaiting on ToListAsync() ?
EDIT
I tried doing a database call before calling this method to warm everything and ensure it doesn't block on establishing the first connection. Also I tried adding a couple of thousand entries to the DbSet and awaiting on SaveChangesAsync and the same thing happens - the UI freezes completely for several seconds.
EDIT 2
I tried another example with the same code and it seems to be working. The difference is that in first example I'm using Code First and SQL CE and in the working example Database First and SQL Server.