1

我正在使用我不熟悉的C++/WRL编写 Windows 10 Store / WinRT 代码。我很想知道如何取消长期挂起的异步操作?

说明它的最好方法是用这个例子:

#include <Windows.Services.Store.h>
#include <wrl.h>

auto onAppLicCompletedCallback = Callback<Implements<RuntimeClassFlags<ClassicCom>, IAsyncOperationCompletedHandler<StoreAppLicense*>, FtmBase>>(
    [](IAsyncOperation<StoreAppLicense*>* operation, AsyncStatus status)
{
    //Asynchronous operation is done
    return S_OK;
});

//'opAppLic' is defined as:
// ComPtr<IAsyncOperation<StoreAppLicense*>> opAppLic;
// ...

//Begin asynchronous operation
HRESULT hr = opAppLic->put_Completed(onAppLicCompletedCallback.Get());
if (SUCCEEDED(hr))
{
    //Keep going ...

    //Say, at some point here I need to cancel 'onAppLicCompletedCallback'
    //but how do I do it?
}

编辑:当我尝试opAppLic->Cancel()按照以下答案中的建议添加时,它给了我以下编译器错误:

1>file-name.cpp(597): error C2039: 'Cancel' : is not a member of 'Microsoft::WRL::Details::RemoveIUnknownBase<T>'
1>          with
1>          [
1>              T=ABI::Windows::Foundation::IAsyncOperation<ABI::Windows::Services::Store::StoreAppLicense*>
1>          ]

我需要QueryInterface那个IAsyncInfo,还是什么?

EDIT2:这就是我得到的opAppLic变量类型:

在此处输入图像描述

不,它没有Cancel方法:

在此处输入图像描述

4

2 回答 2

1

谁也遇到这个。我想我明白了。Remy Lebeau部分正确。我需要做的是IAsyncInfo通过以下方式获得QueryInterface

ComPtr<IAsyncInfo> pAsyncInfo;
if(SUCCEEDED(opAppLic->QueryInterface(__uuidof(pAsyncInfo), &pAsyncInfo)) &&
    pAsyncInfo)
{
    if(SUCCEEDED(pAsyncInfo->Cancel()))
    {
        //Async op was now canceled
        //Also note that `onAppLicCompletedCallback` will be called
        //with `status` set to `Canceled`
    }
}
于 2016-10-30T08:02:18.337 回答
1

IAsyncOperation<TResult>有一个Cancel()方法继承自IAsyncInfo.

您不能取消Completed处理程序本身。它在异步操作完成时触发。您必须取消操作,然后Completed处理程序报告操作的最终状态。

#include <Windows.Services.Store.h>
#include <wrl.h>

auto onAppLicCompletedCallback = Callback<Implements<RuntimeClassFlags<ClassicCom>, IAsyncOperationCompletedHandler<StoreAppLicense*>, FtmBase>>(
    [](IAsyncOperation<StoreAppLicense*>* operation, AsyncStatus status)
{
    //Asynchronous operation is done
    if (status == completed)
    {
        // use results from operation->GetResults() as needed...
    }
    return S_OK;
});

ComPtr<IAsyncOperation<StoreAppLicense*>> opAppLic;
// Begin asynchronous operation that assigns opAppLic...

opAppLic->put_Completed(onAppLicCompletedCallback.Get());

//Keep going ...

//Say, at some point here I need to cancel the operation...
opAppLic->Cancel();
于 2016-10-23T16:06:21.430 回答