2017-04-11 203 views
2

我已經做了一些關於Azure SDK的閱讀,並且爲了取消您似乎需要在cancellation_token中傳遞的任務。取消上傳任務

我上傳的代碼非常簡單:

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

auto task = blockBlob.upload_from_file_async(fullFilePath); 

但是,某些文件我上傳的可能非常大,並且我希望能夠取消這個操作。如果可能的話,我可能也會使用延續,並且還需要所有這些取消。

我遇到的問題是我看不到任何方式將cancellation_token附加到任務。

任何指針?

回答

2

還有a sample code using PPL library,我提到它,並在用於C++的Azure存儲SDK中使用C++ REST SDK中的PPLX庫更改了取消任務的代碼,請嘗試下面的代碼。

/* 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(); 

希望它有幫助。

+0

非常感謝 - 將取消標記傳遞給'then'是我所缺少的,我最終通過'create_if_not_exists_async'創建了一個任務,然後在其中添加了一個帶有取消標記的任務。 –