How to cancel an async query the right way

.net c# entity-framework entity-framework-6 wpf

Question

This is an additional query to this inquiry.

The process of loading data from my database will take 5 to 10 seconds, but I need the GUI to remain responsive and 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;

Despite my best efforts, I simply can't get this to function properly; all it does is load the List, and I am unable to cancel it.

Where is this mistake?

1
4
5/23/2017 12:28:53 PM

Accepted Answer

ZZZ_tmp
4
8/6/2013 7:21:18 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow