Xojo.Net.HttpSocket know how works

How i can wait until the http ends so i can send the next record, i am try to do
If Xojo.Net.HttpSocket then
end if
know how it work

First, stop using Xojo.net.httpsocket. You should be using URLConnection.

If you really want to wait, use SendSync. Otherwise, use Send and you’ll get notified in an Event when it’s done.

But how I wait for the event to end

You don’t. The event fires when the request is done, not without blocking the main thread and doing that will halt the request. Like I said, if you want to wait, just use SendSync.

And if you want to use SendSync, use a thread or you’re going to have a bad time. The only time I’d use SendSync on the main thread is in a quick one-off tool that customers will never see.

Actually, even in a Thread, SendSync can block other threads.

@Alexis_Colon_Lugo make a subclass of URLConnection, add an array property to hold data you want to send.
Each time ContentReceived fires, check if there is pending data to send.
Use a timer to Send the next pending data after 1ms.

If you have a lot of data to send (20+ calls) you could write your own class that holds a pool of URLConnections to send multiple data at once.
Each time a connection ends, re-use it to send the next data.

Do you know the scenarios where this happens? It’s not one I’ve seen myself.

I’ve seen this in a webapp downloading a big PDF file.
I’ve also seen it in a webapp when SendSync couldn’t reach the URL and timedout.

To measure this I made a simple webapp with two pages:
One page that used SendSync.
The second page that displayed a graph and updated every second measuring the delay between each refresh.

Both pages were displayed in a separate Websession.

Regular refresh times where between 980-1050ms.
When using SendSync, refresh time could go up to 15 seconds.

Makes me want to run my own test with desktop. I’d expect matching behavior, but it could be a Mac vs Windows thing too.

1 Like

how to use Send in a console application? thank you.

So… it is a Mac vs Windows thing. Mac does not block, Windows does. That’s just… awesome.

Test project: https://stuff.thezaz.com/SendSyncTest.zip

Edit: Haha… I already discovered this: https://tracker.xojo.com/xojoinc/xojo/-/issues/54467#note_404023

You can either subclass or use AddHandler. Subclassing is easier and doesn’t require as much care, but is often overkill for unless your app is making the same request in many places. With AddHandler, you’d do something like

Sub Socket_ContentReceived(Sender As URLConnection, URL As String, HTTPStatus As Integer, Content As String)
  // Handle Response
End Sub

Event Button1.Pushed
  Var Socket As New URLConnection
  AddHandler Socket.ContentReceived, WeakAddressOf Socket_ContentReceived
  Socket.Send("GET", "URL")
End Event

What makes AddHandler tricky is keeping track of the correct usage of WeakAddressOf vs AddressOf, and storing the socket long enough to complete the request. This is usually done on a property. There’s pros and cons to both techniques.

thanks for big help