This is a follow up question to this question.
I'm trying to load data from my database which will take 5-10 seconds, but I want the GUI to stay responsive and also it should be cancellable.
private CancellationTokenSource _source;
public IEnumerable<Measurement> Measurements { get { ... } set { ... } }
private async void LoadData()
{
_source = new CancellationTokenSource();
using (var context = new TraceContext())
{
Measurements = null;
Measurements = await context.Measurements.ToListAsync(_source.Token);
}
}
private void Cancel()
{
if (_source != null)
_source.Cancel();
}
public RelayCommand ReloadCommand
{
get { return _reloadCommand ?? (_reloadCommand = new RelayCommand(Reload)); }
}
private RelayCommand _reloadCommand;
public RelayCommand CancelCommand
{
get { return _cancelCommand ?? (_cancelCommand = new RelayCommand(Cancel)); }
}
private RelayCommand _cancelCommand;
I've tried a few things, but I just can't get this to work properly, this just loads the List and thats all, I can't cancel this.
Where is the error in this?
CancellationTokenSource
to cancel?ToListAsync
? From a bit of googling, I can't find it as part of MSDN or something. Maybe you wrote it and it contains a bug, so that the token is not being applied correctly?Task
which has transitioned to theCancelled
state) only if an attempt is made toWait()
for the task or observe the result of the task. In this case, the task is controlled by EF and will only transition to that state if EF chooses to. Without knowing their internal details, the beta version may not do this.