Cancel an Asynchronous operation?

  • Thread starter Thread starter Robinson
  • Start date Start date
R

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
 
Back
Top