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:
The general pattern for implementing the cooperative cancellation method is…
public object cancellationTokenSource = null; public async Task SendGetRequest() { using (var cts = new CancellationTokenSource()) { cancellationTokenSource = cts; |
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(); } } |
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(); } |
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 |