实现异步回调
以下代码显示了实现 IMFAsyncCallback 接口所需的基本框架。 在此示例中, Invoke 方法声明为纯虚拟方法。 此方法的实现将取决于要调用的异步方法。 有关详细信息,请参阅 调用异步方法。
#include <shlwapi.h>
class CAsyncCallback : public IMFAsyncCallback
{
public:
CAsyncCallback () : m_cRef(1) { }
virtual ~CAsyncCallback() { }
STDMETHODIMP QueryInterface(REFIID riid, void** ppv)
{
static const QITAB qit[] =
{
QITABENT(CAsyncCallback, IMFAsyncCallback),
{ 0 }
};
return QISearch(this, qit, riid, ppv);
}
STDMETHODIMP_(ULONG) AddRef()
{
return InterlockedIncrement(&m_cRef);
}
STDMETHODIMP_(ULONG) Release()
{
long cRef = InterlockedDecrement(&m_cRef);
if (cRef == 0)
{
delete this;
}
return cRef;
}
STDMETHODIMP GetParameters(DWORD* pdwFlags, DWORD* pdwQueue)
{
// Implementation of this method is optional.
return E_NOTIMPL;
}
STDMETHODIMP Invoke(IMFAsyncResult* pAsyncResult) = 0;
// TODO: Implement this method.
// Inside Invoke, IMFAsyncResult::GetStatus to get the status.
// Then call the EndX method to complete the operation.
private:
long m_cRef;
};
以下代码演示派生自 CAsyncCallback
的类的示例实现:
class CMyCallback : public CAsyncCallback
{
HANDLE m_hEvent;
IMFByteStream *m_pStream;
HRESULT m_hrStatus;
ULONG m_cbRead;
public:
CMyCallback(IMFByteStream *pStream, HRESULT *phr)
: m_pStream(pStream), m_hrStatus(E_PENDING), m_cbRead(0)
{
*phr = S_OK;
m_pStream->AddRef();
m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (m_hEvent == NULL)
{
*phr = HRESULT_FROM_WIN32(GetLastError());
}
}
~CMyCallback()
{
m_pStream->Release();
CloseHandle(m_hEvent);
}
HRESULT WaitForCompletion(DWORD msec)
{
DWORD result = WaitForSingleObject(m_hEvent, msec);
switch (result)
{
case WAIT_TIMEOUT:
return E_PENDING;
case WAIT_ABANDONED:
case WAIT_OBJECT_0:
return m_hrStatus;
default:
return HRESULT_FROM_WIN32(GetLastError());
}
}
ULONG GetBytesRead() const { return m_cbRead; }
STDMETHODIMP Invoke(IMFAsyncResult* pResult)
{
m_hrStatus = m_pStream->EndRead(pResult, &m_cbRead);
SetEvent(m_hEvent);
return S_OK;
}
};
此示例向 Invoke 方法内的事件发出信号。 有关各种选项的讨论,请参阅 调用异步方法。
相关主题