如何取消C ++/WRL中的异步回调函数? [英] How to cancel asynchronous callback function in C++/WRL?

查看:162
本文介绍了如何取消C ++/WRL中的异步回调函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C ++/WRL编写Windows 10 Store/WinRT代码我是新来的.而且我很想知道如何取消长期等待的异步操作?

I'm writing Windows 10 Store / WinRT code using C++/WRL which I'm new to. And I'm curious to know how do I cancel long-pending asynchronous operation?

最好的例子是这个例子:

The best way to illustrate it is with this example:

#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()时,它给了我以下编译器错误:

When I tried to add opAppLic->Cancel() as was suggested in the answer below, it gave me the following compiler error:

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还是什么?

Do I need to QueryInterface that IAsyncInfo instead, or what?

这是我对opAppLic变量类型的了解:

This is what I'm getting for the type of opAppLic variable:

不,它没有Cancel方法:

推荐答案

谁也遇到了这个问题.我想我明白了. Remy Lebeau 部分正确.我需要做的是像这样通过QueryInterface获得IAsyncInfo:

Whoever also runs into this. I think I got it. Remy Lebeau was partially correct. What I needed to do was to obtain IAsyncInfo via QueryInterface as such:

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`
    }
}

这篇关于如何取消C ++/WRL中的异步回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆