How do you kill an Asynchronous Delegate's thread

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::DmDoSomethingHandler);

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;
}
 
E

EMonaco

Mike,

Wouldn't a hung thread generally indicate a problem (that you would want
to correct) with your code? As a general rule having 1 thread terminate
another is not recommended and often not safe as you usually have no way of
knowing what the other thread is doing.

I do know .NET makes things a bit better. On the System.Threading.Thread
object you can call IsAlive() if true then Abort() on the thread object. Now
in the thread you would want something like;

private void MyThread()
{
// Declare objects, vars, etc that you need
// to be accessible in the finally() block here
// before try
try
{
// Thread does all work here
// calls methods, creates objects etc. whatever.

}
catch(System.Threading.ThreadAbortException tae)
{
// Means thread was externally aborted
// could signal clean cancelling, abort etc.
}
catch(System.Exception se)
{
// Means some other exception occurred.
// handle, report or whatever.
}
finally
{
// This is where you'd be sure to close
// or disconnect or whatever, free and
// soforth- all your important resources.

// Remember to be accessible here object
// etc. have to be declared OUTSIDE of this
// try, catch(), finally block!
}
}

Regards,
Erin.
 
M

Mike V

Yeah, the problem is that the blocking calls are from the Xbox API and I
have no control over them. So I have to change my code in order to
accommodate their broken code -- don't you hate that?

I started with the System.Threading.Thread approach but realized just how
much more convenient asynchronous delegates would be -- or so I thought. If
I use Threads, I'll have to come up with a creative way to pass parameters
around, but I suppose I can handle that.

Thanks for your help. I'll try the Thread method and hopefully will have
more luck.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top