Last post Jul 05, 2018 03:19 AM by Yohann Lu
Jul 04, 2018 08:04 AM|sivapooja|LINK
I am retrieving records from busy server. some time it takes more than 3 minutes to retrieve the result. so can the user cancel or kill the current request by clicking the button from the front end? i just need to cancel the last request initiated by the
Jul 04, 2018 11:44 AM|sainyam2003|LINK
try to implement async tasks so that it can be cancelled by user, see example:
Jul 05, 2018 03:19 AM|Yohann Lu|LINK
I am retrieving records from busy server. some time it takes more than 3 minutes to retrieve the result. so can the user cancel or kill the current request by clicking the button from the front end? i just need to cancel the last request initiated by the client.
You can use the CancellationTokenSource
signals to a CancellationToken that it should be canceled.
The following code for your reference.
// Declare a System.Threading.CancellationTokenSource.
private async void test(object sender, EventArgs e)
// Instantiate the CancellationTokenSource.
cts = new CancellationTokenSource();
// ***Set up the CancellationTokenSource to cancel after 2.5 seconds. (You
// can adjust the time.)
cts = null;
// You can still include a Cancel button if you want to.
private void cancelButton_Click(object sender, RoutedEventArgs e)
if (cts != null)
protected async Task accesswebapiasynccan(CancellationToken ct)
using (var client = new System.Net.Http.HttpClient())
// HTTP POST
var baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
client.BaseAddress = new Uri(baseUrl);
var content = new StringContent(JsonConvert.SerializeObject("Test"), Encoding.UTF8, "application/json");
var response = client.PostAsync("/api/Values", content, ct).Result;
string s = await response.Content.ReadAsStringAsync();
For more detailed, please see:
Cancel Async Tasks after a Period of Time (C#)