Store in a field whether the cancel-button was clicked or not:
bool hasUserCancelled = false;
And reset this field before you start:
hasUserCancelled = false;
cts = new CancellationTokenSource();
cts.CancelAfter(5000);
Set it in the cancel-button click handler:
private void OnCancelClick(object sender, RoutedEventArgs e)
{
hasUserCancelled = true;
cts.Cancel();
cts.Dispose();
}
The information that you wanted is now available in the catch:
catch (TaskCanceledException taskCanceledException)
{
Debug.WriteLine(new { hasUserCancelled });
}