【问题标题】:Cancelling an upload task取消上传任务
【发布时间】:2017-04-11 15:03:09
【问题描述】:

我已经阅读了一些有关 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 附加到任务的方法。

任何指针?

【问题讨论】:

    标签: c++ azure-blob-storage casablanca cpprest-sdk


    【解决方案1】:

    a sample code using PPL library,我参考了它并更改了使用C++ REST SDK中的PPLX库取消任务的代码,用于C++的Azure Storage SDK,请尝试下面的代码。

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

    希望对你有帮助。

    【讨论】:

    • 非常感谢 - 将取消令牌传递给 then 是我所缺少的,我最终通过 create_if_not_exists_async 创建了一个任务,然后在其中添加了一个带有取消令牌的 then。
    猜你喜欢
    • 2022-01-10
    • 2016-03-18
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多