How to return value from Xojo.Net.HTTPSocket subclass

Hi. I want to make a call to a method within a subclassed socket and handle the return value as follows:

Dim socket As New CustomSocket
Dim Result As Boolean = socket.myMethod("some parameter")

Because the new framework socket is asynchronous I am confused how to handle the polling of myMethod waiting for a response to return because I do not want to execute any other code after the initial socket call and want to wait for the response before proceeding to handling it.

I understand I could do a Do/While loop within myMethod waiting for PageReceievd or Error event etc but this would freeze up the app and hog the processing power.

I intend to add a customTimer to the socket subclass to handle the timeout and socket.close.

Any advice would be appreciated. Thanks.

You don’t poll, and don’t use a tight loop to wait . You simply place the rest of the code in a method called by PageReceived.

In event programming, you got to drop the old sequential programming.

Yes, I have all the code in PageReceived right now but I want to try and do it the other way and wanted to know if that was possible.

Polling and loops to wait an event are old thinking. Event programming simply does not work like that. Even if you implemented a tight loop, it would freeze the entire program and most probably prevent the PageReceived event from firing.

The only way to do it without freezing everything would be to move the code to a thread. But at this point, it adds undue complexity.

Keep it simple is the way to bug free programming.

Thanks, Michel. It’s not the ideal solution for this particular situation but I will certainly take your advice on board. Appreciated.

You might want to rethink your logic then… Instead of waiting for the result from the socket to continue in the current method, just move that code to an event and the socket will call the event and take care of all of the waiting, without locking up your program…

Yes, exactly. Thanks. That’s what I will have to do unfortunately. I know it’s viable it’s just I had my working system set up the other way on app launch and needed to wait for the result before deciding how to handle it and so I just now have to rethink the order of events and where to place the code etc. It’s a hassle but doable.

Well, if you really regret the ancient times, just go back to classic synchronous httpSocket.

As I said I need HTTP 1.1 protocol and better security so have no choice.

Well, then once you have adapted your code you will be safe.

Technology is a never ending adaptation challenge for programmers.

Yes, for sure :slight_smile: