M
Mike V
Hello,
I have read several articles about using Asynchronous Delegates and I've
implemented them successfully, but I am still unsure how I would terminate a
thread pool thread created through a call to BeginInvoke() in the very
likely (in my case) event that the delegated method hangs.
Every article I've read seems to belabor the point that EndInvoke() must be
called whenever BeginInvoke() is called. But if the delegate never returns,
calling EndInvoke() will cause the calling thread to hang, correct? That's
certainly not what I want. I tried getting ahold of the WaitHandle
associated with the AsyncResult that BeginInvoke() returns, but calling its
Close() method doesn't seem to actually kill the thread (it was a stab in
the dark on my part). I've posted my code snippet below just in case it
proves helpful.
Any help is greatly appreciated! I've been stuck on this for some time now.
Int32 retVal;
try
{
DmDoSomethingDelegate* pDelegate = new DmDoSomethingDelegate(0,
CDmWrappers:mDoSomethingHandler);
Object* pDummyObject = new Object();
IAsyncResult* pAsyncResult = pDelegate->BeginInvoke(strFilename, NULL,
pDummyObject);
WaitHandle* pWaitHandle = pAsyncResult->AsyncWaitHandle;
Boolean bFunctionCompleted = pWaitHandle->WaitOne(5000, false);
// The Dm method completed successfully
if (bFunctionCompleted)
{
retVal = pDelegate->EndInvoke(pAsyncResult);
}
else
{
// The function timed out, so return a timeout error
pWaitHandle->Close(); /// *** Does this terminate the thread? It
doesn't seem to. ***
retVal = HRESULT_FROM_WIN32(ERROR_TIMEOUT);
}
}
catch (...)
{
retVal = -1;
}
I have read several articles about using Asynchronous Delegates and I've
implemented them successfully, but I am still unsure how I would terminate a
thread pool thread created through a call to BeginInvoke() in the very
likely (in my case) event that the delegated method hangs.
Every article I've read seems to belabor the point that EndInvoke() must be
called whenever BeginInvoke() is called. But if the delegate never returns,
calling EndInvoke() will cause the calling thread to hang, correct? That's
certainly not what I want. I tried getting ahold of the WaitHandle
associated with the AsyncResult that BeginInvoke() returns, but calling its
Close() method doesn't seem to actually kill the thread (it was a stab in
the dark on my part). I've posted my code snippet below just in case it
proves helpful.
Any help is greatly appreciated! I've been stuck on this for some time now.
Int32 retVal;
try
{
DmDoSomethingDelegate* pDelegate = new DmDoSomethingDelegate(0,
CDmWrappers:mDoSomethingHandler);
Object* pDummyObject = new Object();
IAsyncResult* pAsyncResult = pDelegate->BeginInvoke(strFilename, NULL,
pDummyObject);
WaitHandle* pWaitHandle = pAsyncResult->AsyncWaitHandle;
Boolean bFunctionCompleted = pWaitHandle->WaitOne(5000, false);
// The Dm method completed successfully
if (bFunctionCompleted)
{
retVal = pDelegate->EndInvoke(pAsyncResult);
}
else
{
// The function timed out, so return a timeout error
pWaitHandle->Close(); /// *** Does this terminate the thread? It
doesn't seem to. ***
retVal = HRESULT_FROM_WIN32(ERROR_TIMEOUT);
}
}
catch (...)
{
retVal = -1;
}