Home All Groups Group Topic Archive Search About

Cancel an Asynchronous operation?

Author
14 Sep 2006 3:05 PM
Robinson
I can start an Asynchronous operation against a data source with
SQLCommand.BeginExecuteReader, allowing me to loop, checking for user
cancellation before the operation has completed, but how then to cancel the
SQLCommand if the user wishes to?  The "Cancel" method states:

     ".......... The Cancel method cannot be used to cancel a pending
asynchronous operation."

The scenario I'm thinking about here concerns a potentially long-running
operation the user might start not realising how long it might take, despite
a UI warning, then deciding to cancel it.  Because I don't know if the
operation will take seconds or minutes, I can't set an appropriate timeout
other than "0", which means the user must be allowed to cancel it.  Is there
any way to stop an asynchronous operation "mid-flow" so to speak, without
running it on a thread and aborting the thread (not a very clever thing to
do)?  What about closing the underlying connection or generating an
exception, will that stop the pending operation?


Robin

Author
14 Sep 2006 8:41 PM
Mike McIntyre
There is a discrepency in the MSDN documentation.

One of the key ADO.NET team members, Pablo Castro, does recommend using the
Cancel command and it does seem to work just fine.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/async2.asp

--
Mike

Mike McIntyre [MVP]
http://www.getdotnetcode.com


Show quoteHide quote
"Robinson" <itoldyounottospamme@nowmyinboxisfull.com> wrote in message
news:eebr3v$6ng$1$830fa17d@news.demon.co.uk...
>I can start an Asynchronous operation against a data source with
>SQLCommand.BeginExecuteReader, allowing me to loop, checking for user
>cancellation before the operation has completed, but how then to cancel the
>SQLCommand if the user wishes to?  The "Cancel" method states:
>
>     ".......... The Cancel method cannot be used to cancel a pending
> asynchronous operation."
>
> The scenario I'm thinking about here concerns a potentially long-running
> operation the user might start not realising how long it might take,
> despite a UI warning, then deciding to cancel it.  Because I don't know if
> the operation will take seconds or minutes, I can't set an appropriate
> timeout other than "0", which means the user must be allowed to cancel it.
> Is there any way to stop an asynchronous operation "mid-flow" so to speak,
> without running it on a thread and aborting the thread (not a very clever
> thing to do)?  What about closing the underlying connection or generating
> an exception, will that stop the pending operation?
>
>
> Robin
>
>
Author
15 Sep 2006 8:36 AM
Robinson
"Mike McIntyre" <mik***@getdotnetcode.com> wrote in message
news:O65Xg4D2GHA.4476@TK2MSFTNGP02.phx.gbl...
> There is a discrepency in the MSDN documentation.
>
> One of the key ADO.NET team members, Pablo Castro, does recommend using
> the Cancel command and it does seem to work just fine.
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/async2.asp
>




Ahhhaa.   Thats good news.  Thanks a million.