I'm following a walkthrough on how to combine EntityFramework with WPF. I decided to play around with async/await while I'm at it, because I've never really had a chance to use it before (we've just moved to VS2013/.NET 4.5 from VS2010/.NET 4.0). Getting the save button handler to be async was a breeze, and the UI remained responsive (I can drag the window around) while SaveChangesAsync() is awaited. In the window load handler, however, I ran into a small snag.
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
EnterBusyState();
var categoryViewSource = (CollectionViewSource)FindResource("categoryViewSource");
_context = await Task.Run(() => new Context());
await Task.Run(() => _context.Categories.Load());
//await _context.Categories.LoadAsync();
categoryViewSource.Source = _context.Categories.Local;
LeaveBusyState();
}
When I use the first way of loading _context.Categories, the UI remains responsive, but when I substitute with the commented-out line below, the UI freezes for a brief moment while the entities load. Does anyone have an explanation on why the former works while the latter doesn't? It's not a big deal, it's just bugging me that the second line doesn't work when, at least according to what I've researched about async/await so far, it should.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…