What does socket.poll do

That is, does it:

  1. check a status and return immediately

  2. suspend the thread until a status change occurs and then return

  3. spin on a status using up CPU cycles until a status change occurs and then return

  4. ???

Thanks,

2 is pretty much what happens in a synchronous get with an HTTPSocket. But then, no need to poll.

3 definitely not. Should be replaced by PageReceived event.

Polling is kind of an ancient way of doing things. In old procedural languages, it was done in tight loops. In Xojo, it should rather be done with a timer, to avoid holding an event and getting the beach ball of death in Mac OS X or the Windows “this application has become unresponsible”.

If you can wrap your head about asynchronous events, you will enjoy a much smoother ride. Especially since the new framework is asynchronous.

You can think of it like this: in Async mode, the framework polls the socket for you as part of the event loop. If your code prevents the event loop from happening (tight loop, etc) then you must poll the socket yourself. As a side effect, it allows you to use the socket synchronously, by blocking the event loop (which may prevent other important things from happening) and doing your own poll loop.

This is all very well but some of what I want to do is inherently sequential. I want to connect to a remote host, then logon to it, then ask it how many emails it has waiting for me, then consult my database to see which ones I already downloaded, and then download the new ones, one by one. And as they arrive I’ll need to update the database(s) and also the UI, which will be fun as I can’t do that updating from a thread. Oh, and the whole process as described above may happen asynchronously to several different hosts at the same time.

If I use events for this, I’ll need to know how, in a thread, I can wait for an event (timers is a very hacky way to do it), so that when the event fires I can resume the thread. It looks in principle like I can sleep the thread and have it resumed from the event handler but I’ve not done that successfully yet.

My existing app, written in JavaScript and PHP, already does all this with the PHP side doing all the sequential stuff and the JS side doing all the async event-driven UI stuff. But I felt it was time it all lived inside one app :slight_smile:

If you use events for this, you don’t need a thread. You need to maintain some state info that tells you how to handle the data as it comes in.

It’s a different way of thinking about programming, but it works very well.

[quote=231713:@Tim Hare]If you use events for this, you don’t need a thread. You need to maintain some state info that tells you how to handle the data as it comes in.

It’s a different way of thinking about programming, but it works very well.[/quote]
Do you have a simple example you can share.

Hope I am not hijacking this thread.

I have a similar need where at one point I need to pause or wait for x numbers of seconds.
I am just having a difficult time wrapping my head around the state method.

For example If i send too many requests to a server I will get a http status 502 instead of 200 if I send another request based on the 502 I get another 502 if I delay the next call a second or two I will get a 200 and continue with no issue.

I have been using a tight loop with do Events for the delay which I know is discouraged, I would love to finally understand this whole state thing.

Tim, in your situation the “state” could be just an array of requests to send. Use a timer and in the Action event of the timer, send the next request and remove it from the array.

well, one way is like this…

subclass socket and have say four functions, connect,listMail,GetById, disconnect

then property mailId() as string, state, lastMail

in the connect you just connect to the server

then in the listmail you check if you are connected, and get the list into the array.

in getbyid you put what is returned into lastmail

then you loop through the array doing getbyId and process the results

you use state to say what you are doing, and in the datarecieved event you know what the response is (list or email)