Cancel an Asynchronous operation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robinson

    #1

    Cancel an Asynchronous operation?

    I can start an Asynchronous operation against a data source with
    SQLCommand.Begi nExecuteReader, 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


  • Mike McIntyre

    #2
    Re: Cancel an Asynchronous operation?

    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/de...tml/async2.asp

    --
    Mike

    Mike McIntyre [MVP]



    "Robinson" <itoldyounottos pamme@nowmyinbo xisfull.comwrot e in message
    news:eebr3v$6ng $1$830fa17d@new s.demon.co.uk.. .
    >I can start an Asynchronous operation against a data source with
    >SQLCommand.Beg inExecuteReader , 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
    >
    >

    Comment

    • Robinson

      #3
      Re: Cancel an Asynchronous operation?


      "Mike McIntyre" <mikemc@getdotn etcode.comwrote in message
      news:O65Xg4D2GH A.4476@TK2MSFTN GP02.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/de...tml/async2.asp
      >



      Ahhhaa. Thats good news. Thanks a million.


      Comment

      Working...