views:

201

answers:

2

Hello!

I have a custom control that make asynchronous Web Services calls. I wonder if I can dispose the control while an asynchronous call hasn't ended: I make the call, and before I get the response I dispose the object.

What must I do before I get the response to dispose the custom control safetly?

Thank you!

A: 

It is generally not a good idea to abort another thread (it is more CPU expensive than just allowing it to finish).

What you probably want to do, is in your Dispose() method set a flag that says the control is disposed. Then when your request completes, check the flag and ignore the result of your request if your request was aborted.

If you need specific help, post some of your code and we'll help you out.

Jonathan.Peppers
+1  A: 

Use CancelAsync() method. Check this article.

Viktor Jevdokimov
Note that this cancels the client side of the request only; the server side will just continue (but the response will go into nirvana).
Jeroen Pluimers