How to cancel an asynchronous operation in Silverlight / WCF?

I call the asynchronous service from my Silverlight application, and I want to be able to cancel this call after creating it. There is an option for e.Cancelled after the service terminates (i.e. if e.Cancelled Then), but how did you determine that it was canceled to true after you called it? How to cancel an asynchronous call?

Let me clarify a bit ... what I'm trying to do is call the same method twice, one by one, and get the results of the last call to my collection. If I call the asynchronous method twice, there is no guarantee that the second call will return first, so that I can get the results of the first call that arrives last and have incorrect results in my collection. So what I would like to do is cancel the first call when I make the second so that I don't get the results from the first call. Seeing the β€œCanceled” flag in the completed event, I suppose you should do this. But how?

+4
source share
2 answers

This is asynchronous ... the transfer is transmitted to the remote server and is not returned until the server is done with it.

Basically, the server will continue to work, but you do not need to wait for a response. Disable the processed event handler and pretend that it has never been called. This will have the effect of canceling the operation.

If you really need to cancel something on the server, you will need to make another call to the server to cancel the first call. Assuming the first call is very slow, this might be possible.

Update (as the question changes)

In that case, if you specify, the server will cancel the operation if the second one goes, and not before the client. e.Cancelled is configured on the server side.

But...:)

You have discovered the usability problem of the client. You should also not delay sending any service request until downtime occurs. Thus, quick selection will not result in multiple service calls.

Also ...:>

You can also send a sequence number to your office calls and return this as part of the result. Then you will find out if this is the last request or not.

+5
source

It seems like you really want to ignore the answers of all but the most recent call.

Set a unique identifier (maybe request #, a pointer, timestamp or something else) with the request and make sure that the service sends the same value back. Keep the identifier of the last request and ignore the response that does not match this identifier.

This will be safer than canceling the first request, because if the service has already begun to send a response before the cancellation request occurs, you will still receive your error condition.

+1
source

All Articles