Help with Delegates: AsyncWaitHandle.Close

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
Okay, in C# 2.0 I'm having trouble figure out how to close down a handle properly when it has timed out:

C#:
//Invoke delegate with callback
IAsyncResult iar = write.BeginInvoke(format, arg, callback, null);
//Give it 30 seconds to complete
iar.AsyncWaitHandle.WaitOne(30000, true);
//If it hasn't completed, terminated the call
if(!iar.IsCompleted)
{
	iar.AsyncWaitHandle.Close();
}
else
{
	//Properly clean up
        write.EndInvoke(iar);
}

So after closing the handle anything in the delegate that is currently executing finishes executing and the system still tries to fire the IAsync delegate (callback) which generates an error because the wait handle has been closed.

So really there's two things...how do I stop execution of the asynchronous call IMMEDIATELY, and second keep the asynchronous call from being executed...I was looking for a 'Cancel' method, but the 'Close' method is the best I could get.

I may be also doing this wrong too...asynchronous calls aren't my strong point. Also I know you are suppose to call EndInvoke when you use BeginInvoke, but with the Close method I couldn't...if that's not correct please let me know.
 
Back
Top