JasinCole
Well-known member
- Joined
- Feb 16, 2023
- Messages
- 66
- Programming Experience
- 1-3
I am having a hard time understanding async/await code in my Winui3 app. I have a data service with the following code which I believe is correct approach using EF
Then I have this in my viewmodel. But it doesn't work as I expect. I've done significant searching but it seems I am missing part of the equation. Why does this block the UI thread? What's the better approach?
I also tried to use the TaskNotifier according to microsoft, but regardless of my attempt it always blocks the UI, which probably means I'm and idiot and don't understand async/await properly. Would appreciate a little understanding on this subject.
C#:
public async Task<IEnumerable<Employ>> GetCurrentEmployeesAsync()
{
return await SageDbContext.Employs
.Where(e => e.Status == (byte)Status.Current)
.ToListAsync();
}
Then I have this in my viewmodel. But it doesn't work as I expect. I've done significant searching but it seems I am missing part of the equation. Why does this block the UI thread? What's the better approach?
C#:
{
private readonly IUnitOfWork _srvc;
private readonly Task _initTask;
[ObservableProperty]
private IEnumerable<Employ> _employees;
[ObservableProperty]
private Employ _selectedEmployee;
[ObservableProperty]
private string _firstName;
[ObservableProperty]
private string _lastName;
public EmployeeViewModel(IUnitOfWork srvc) {
_srvc = srvc;
_initTask = LoadEmployees();
}
public async Task LoadEmployees()
{
Employees = await _srvc.Employees.GetCurrentEmployeesAsync();
}
}
I also tried to use the TaskNotifier according to microsoft, but regardless of my attempt it always blocks the UI, which probably means I'm and idiot and don't understand async/await properly. Would appreciate a little understanding on this subject.