4

I am programming in .net WPF.

I have a (third party) API which implements the Begin/End asynchronous call pattern. The BeginWork() function returns an IAsyncResult.

However, there is no apparent method to cancel/abort the call, once done.

Is there a way to have such a job stopped? If this requires the library author to provide explicitly for a cancel method, what is a way to kill this job, even ungracefully? I really need to be able to stop it somehow, as a single job may take hours to complete!

Thanks!

Palantir
  • 23,820
  • 10
  • 76
  • 86
  • Well, which specific API is it? But: if they don't expose a cancel, I would not assume you can cancel it. – Marc Gravell May 11 '13 at 09:16
  • @ilansch it is not clear to me that a WCF post is going to necessarily apply here... There are lots of async / IAsyncResult APIs, and they behave differently – Marc Gravell May 11 '13 at 09:18
  • It's an Amazon's S3 upload. But I have made a specific question yesterday about that library, and got no answer. So I instead thought about a generic way to kill an async task. http://stackoverflow.com/questions/16485629/s3-multipart-upload-how-can-i-cancel-one – Palantir May 11 '13 at 09:22
  • In theory you could [create a separate AppDomain](http://msdn.microsoft.com/en-us/library/ms173138%28v=vs.100%29.aspx) in which to run the thread, and then you could shut down the entire app domain. But that's a lot of work and [communicating between app domains needs WCF](http://stackoverflow.com/questions/8972352/communication-between-appdomains) or some similar mechanism. – Matthew Watson May 11 '13 at 09:28

1 Answers1

3

If the implementation does not include specific code allowing for cancellation, then it is quite likely that you can't cancel it. Not everything can be logically cancelled cleanly, especially if it involves external resources. But also if the code to cancel it simply hasn't been written. .you could try randomly killing threads, but this will doom your process - basically this would be the same as deciding to kill the entire process half way through. It will stop the work, but it could leave things in a complete mess if it isn't transactional.

If I was you I would (one of):

  • ask the 3rd party for a supported cancellation API
  • don't start it unless you are sure
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Also worth noting @Palantir could maybe support logical cancellation, whereby it looks to the user as if the operation has been canceled when in fact it's still running in the background and the result will be discarded when it does eventually complete. – Kent Boogaart May 11 '13 at 12:11
  • That can work if the operation is quick enough. But if the operation can take hours, then it is unfortunately no longer acceptable :( – Palantir May 11 '13 at 13:19