我从DLL调用以下代码,从Windows应用商店应用在Windows 10上执行WinRT操作。该代码使用WRL:
#include <Windows.Services.Store.h>
#include <wrl.h>
auto onAppLicCompletedCallback = Callback<Implements<RuntimeClassFlags<ClassicCom>, IAsyncOperationCompletedHandler<StoreAppLicense*>, FtmBase>>(
[](IAsyncOperation<StoreAppLicense*>* operation, AsyncStatus status)
{
if(status == AsyncStatus::Completed)
{
//Do actions of the async operation
...
}
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'
//after a user clicks Cancel button via a UI, so I do this:
ComPtr<IAsyncInfo> pAsyncInfo;
if(SUCCEEDED(opAppLic->QueryInterface(__uuidof(pAsyncInfo), &pAsyncInfo)) &&
pAsyncInfo)
{
pAsyncInfo->Cancel();
}
}
//And now unload DLL
但是当我调用IAsyncInfo::Cancel()
方法并在之后立即卸载DLL时,会导致竞争条件并且有时会使应用程序崩溃。
纯粹通过实验我注意到,在致电IAsyncInfo::Cancel()
后,框架调用我的onAppLicCompletedCallback
方法并将status
设置为AsyncStatus::Canceled
。但是这个调用也在IAsyncInfo::Cancel()
方法返回后很长时间内异步发生。
所以我想知道,有没有办法等待所有WinRT异步回调完成运行才能继续卸载我的DLL?