Prematurely Abort Asynchronous Operation

jeremy_ckw

Member
Joined
Apr 11, 2007
Messages
5
Programming Experience
3-5
Hi,

I've been searching around for a way to abort an asynchronous operation but couldn't find any good leads. Wonder if anyone can help me with this?
 
With what code do you start the asynchronous operation?
 
Hi,

I've been searching around for a way to abort an asynchronous operation but couldn't find any good leads. Wonder if anyone can help me with this?

SImply return from the method that was called as the start..

If your class has a DoWork() method, and you started it with something like:

System.Threading.ThreadPool.QueueUserWorkItem(myWorkerObject.DoWork)


Then just Return from DoWork() and that's it..
 
If you started it by a delegate you can stop it by calling EndInvoke method on same delegate instance.
 
If you started it by a delegate you can stop it by calling EndInvoke method on same delegate instance.

Unfortunately, EndInvoke doesn't actually stop the operation. It merely pauses the calling thread to wait for the asynchronous operation to complete.
 
I think I have not been clear enough about what I need.

I'm actually trying to call FileStream.BeginRead which is a asynchronous call to method FileStream.BeginRead. While the reading operation is executed, if the amount of time taken exceeds a pre-configured value, I want to abort this operation.

I hope I'm clear enough now. Sorry for the trouble.
 
Unfortunately, EndInvoke doesn't actually stop the operation. It merely pauses the calling thread to wait for the asynchronous operation to complete.
oh, that's true :eek: but you could use the returned waitHandle from beginInvoke and call its WaitOne with 0 timeout.
 
oh, that's true :eek: but you could use the returned waitHandle from beginInvoke and call its WaitOne with 0 timeout.


John,

The WaitHandle from the IAsyncResult doesn't stop it as well. What the WaitHandle.WaitOne does, is blocking the calling thread for the amount of time specified in the parameters. By putting zero in the parameter, we are actually asking the calling thread NOT to wait for the asynchronous method to return.
 
Some bad test code here, I will investigate further. Perhaps run the thread and abort it?
 
I think we should wait until jeremy actually tells us how he started the op? (ref: post 2, john's first reply)
 
He said "FileStream.BeginRead". My research so far doesn't give option to stop an asynchronous delegate, only thread can.
 
I actually used waithandle.waitone with timeout from a Console app recently to abort the async calls, but in that case the console terminated after the timeout and in effect stopped all its unfinished threads.
 
Back
Top