Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Okay, in C# 2.0 I'm having trouble figure out how to close down a handle properly when it has timed out:

 

//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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...