Wednesday, June 30, 2004

Cancelling a SOAP function call

A newsgroup user asked "Is it possible to cancel waiting for the webservice to return the result of a function?" To be extremely brief: Yes.

Less succinctly, the answer lies in the ingenious use of threads. Firstly, if you're going to have a single threaded application, you can't stop a function call while it's running. It's like you're reading the paper in your car, stopped at a traffic light, alone; and the light's just turned green. You have no idea because well, there's no one around to tell you. (for the record, I've never done this)

So in order to cancel a function call, you need to put that function call in a separate thread, and in the main UI thread (where your form is) - implement a cancel button that will somehow terminate the function call. How does one do that? The SOAP call that you make actually waits for the Internet call to return - and you have no way to tell this code to stop gracefully.

Now, a HTTPRio uses WinInet by default, and you get the handle to the Request in the HttpWebNode.OnBeforePost event. You can call InternetCloseHandle with this handle anytime - this will raise an exception in the thread - with a "The operation has been canceled...." message. The thread will terminate, and your function call get cancelled.

Note: the server call will continue to run on the server till termination. To actually terminate whatever's happening at the server you'll need to use some other logic (like a server thread that can be terminated from another SOAP call or some kind of constant check on the request validity).

I've got a test application here that demonstrates what I'm talking about. Download it at http://www.agnisoft.com/downloads/SoapCancelWait.zip. And as usual, tell me what you think!

0 Comments:

Post a Comment

<< Home