I have seen a similar question being asked here but it does not seem quite right for my scenario.
We have a UI that can execute a request and if the user wants to execute the request again (with a different query parameter) we want to abandon the initial request, ignore its response and only use the latest requests response.
At the moment I have:
private readonly IDataService _dataService;
private readonly MainViewModel _mainViewModel;
private CancellationTokenSource _cancellationTokenSource;
//Constructor omitted for brevity
public async void Execute()
{
if (_cancellationTokenSource != null)
{
_cancellationTokenSource.Cancel();
}
_cancellationTokenSource = new CancellationTokenSource();
try
{
string dataItem = await _dataService.GetDataAsync(_mainViewModel.Request, _cancellationTokenSource.Token);
_mainViewModel.Data.Add(dataItem);
}
catch (TaskCanceledException)
{
//Tidy up ** area of concern **
}
}
This seems to function well and I have a nice and responsive UI but I have a scenario that concerns me:
- A request is made by the user
- The user makes a new request which cancels the original request
- The new request returns before the original cancelled request throws its exception populating the UI with the currently required data
- The exception is thrown and clean up occurs overwriting the new requests output
This may be very rare but I can see it as a possibility unless my understanding of this is wrong.
Is there any way to ensure that if a Task is cancelled via a cancellation token request and a new Task is started the cancellation happens before the new task is started/returns execution without blocking the UI thread?
Any reading to expand my understanding on this would be most appreciated.