我之前问过类似的问题,但那里的答案不符合我的要求。让我解释。
我从 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
set to AsyncStatus::Canceled
。IAsyncInfo::Cancel()
但是这个调用也会在方法返回后很长时间异步发生。
所以我想知道,有没有办法等待所有 WinRT 异步回调完成运行,然后才能继续卸载我的 DLL?