3

I've done some reading regarding the Azure SDK and in order to cancel a task you seemingly need to pass in a cancellation_token.

My upload code is very simple:

azure::storage::cloud_block_blob blockBlob = container.get_block_blob_reference(fileLeaf.wstring());

auto task = blockBlob.upload_from_file_async(fullFilePath);

However, some files I upload are potentially very large, and I would like to be able to cancel this operation. I'll probably also likely use continuations and would need all those cancelling too, if that's possible.

The problem I'm having is I can't see any way of attaching a cancellation_token to the task.

Any pointers?

Dave F
  • 973
  • 9
  • 19

1 Answers1

4

There is a sample code using PPL library, I refered to it and changed the code for canceling task using PPLX library within C++ REST SDK which be used for Azure Storage SDK for C++, please try the code below.

/* Declare a cancellation_token_source and get the cancellation_token, 
 * please see http://microsoft.github.io/cpprestsdk/classpplx_1_1cancellation__token__source.html
*/
#include <pplxtasks.h>
cancellation_token_source cts;
auto token = cts.get_token();

//Pass the cancellation_toke to task via then method, please see https://msdn.microsoft.com/en-us/library/jj987787.aspx
task.then([]{}, token).wait();

// Cancel the task
cts.cancel();

Hope it helps.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Many thanks - passing the cancellation token into `then` was what I was missing, I ended up creating a task via `create_if_not_exists_async` then adding a then with a cancellation token in it. – Dave F Apr 12 '17 at 12:34