Cancelling asynchronous requests

In Windows SAP Mobile Platform SDK (hereinafter referred to as “SMP SDK”) SP09, several new networking features were introduced.  This was possible because Windows SDK now uses Windows.Web.Http.HttpClient internally. Prior to SP09, Windows SDK used System.Net.Http.HttpClient internally.  The new networking library provides the ability to send cancellation requests on all Windows platforms.

Note:  Due to this change, the public API of the networking component has changed for Windows Store apps.  Please refer to the blog by Zoltan Farkas on changes required to migrate an existing Windows application from SMP SDK SP08 (or lower) to SMP SDK SP09 (and higher).

Starting with .NET Framework 4, the .NET Framework uses a unified model for cooperative cancellation of asynchronous or long-running synchronous operations that involves two objects:

  • A CancellationTokenSource object, which provides a cancellation token through its Token property and sends a cancellation message by calling its Cancel or CancelAfter method
  • A CancellationToken object, which indicates whether cancellation is requested

Steps involved

The general pattern for implementing the cooperative cancellation method is…

  • Instantiate a CancellationTokenSource object, which manages and sends cancellation notification to the individual cancellation tokens.

public object cancellationTokenSource = null;

public async Task SendGetRequest()

{

   using (var cts = new CancellationTokenSource())

   {

      cancellationTokenSource = cts;

  • Pass the token returned by the CancellationTokenSource.Token property to each task or thread that listens for cancellation.

using (var cts = new CancellationTokenSource())

{

   cancellationTokenSource = cts;

   using (var client = new SAP.Net.Http.HttpClient())

   {

      var responseMessage = await client.SendAsync(() =>

         new HttpRequestMessage(HttpMethod.Get, new  Uri(http://127.0.0.1/Northwind/NorthwindService.svc/Products?$top=1000)),

         HttpCompletionOption.ResponseContentRead, cts.Token

);

      message = await responseMessage.Content.ReadAsStringAsync();

   }

}

  • Call the CancellationTokenSource.Cancel method to provide notification of cancellation. This sets the CancellationToken.IsCancellationRequested property on every copy of the cancellation token to true.

private async void CancelTask(object sender, RoutedEventArgs e)

{

   if (cancellationTokenSource == null)

      return;

   ((CancellationTokenSource)cancellationTokenSource).Cancel();

   cancellationTokenSource = null;

   await new Windows.UI.Popups.MessageDialog(“GET request cancelled”).ShowAsync();

}

  • Provide a mechanism for each task or thread to respond to a cancellation request.  No coding required for this step.  The SMP SDK handles all the logic involved in cancelling an operation based on CancelToken.IsCancellationRequested property.
  • Call the Dispose method when you are finished with the CancellationTokenSource object. The using statement provides a convenient syntax that ensures the correct use of IDisposable objects.

Please find attached the source code for a sample Windows application that cancels an asynchronous request. 

In the next blog, I will talk about how we can track the progress of a GET request.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply