How to ensure async data has been received before returning a result?

My function requests and returns data, but the data is received asynchronously. On Mac it works perfectly, but of course Windows is giving me troubles. I need to make sure the data has been received before returning the value in the function. I’m a little bit stuck on how to do this. If I use a while loop and a timestamp, the app will lock up in the while loop since it can’t receive the data and update the timestamp while in the loop. I’m not quite sure where to go with this…

No, the data cannot be received synchronously.

What are you receiving it from? Can you use Poll?

Unfortunately not. The data comes in as a result of a HTMLViewer.Status change.

What happens, or does not happen on Windows ?

Is the StatusChanged the signal that data is available, or that it is on it’s way ?

When the HTMLViewer StatusChanged event happens it passes the status to a property. What happens is the function calls JavaScript in the HTMLViewer and returns the value of the property. I need to make sure that property has actually been set and changed before returning it. Mac happens to get the property set before returning it, but on Windows it returns the property which has usually not changed yet.

Do you mean Windows JS reports a value for a control before it has been set ?

Can’t you come back and verify it is what it is supposed to be ? Eventually after a small delay ?

IE is so bug ridden, you may want to try setting the rendered to WebKit. It may very well solve the issue.

[quote=160118:@Tim Parnell]My function requests and returns data, but the data is received asynchronously.
[/quote]

You realize these statements are contradictory. You should really rethink your approach and make it event driven.

I do, however there’s so little to work with because HTMLView cannot return the value of a JavaScript.

[quote=160171:@Michel Bujardet]Do you mean Windows JS reports a value for a control before it has been set ?

Can’t you come back and verify it is what it is supposed to be ? Eventually after a small delay ?

IE is so bug ridden, you may want to try setting the rendered to WebKit. It may very well solve the issue.[/quote]
No. Windows returns the value of the Xojo Property before the HTMLViewer completes the execution of the JavaScript that results in the StatusChange event which is supposed to update that property. The Renderer is already set to WebKit because I don’t trust IE to handle even simple things like html5 properly.

I can’t verify what it’s supposed to be, because the value changes and can be anything at all.
The small delay is exactly what I’d like to accomplish in Xojo so that the HTMLView has a chance to execute the javascript.

I’m working on an object that one can just drop in and use very similar to TextArea. It returns HTML and has much of the functionality of rich text from TextArea. It works perfectly on Mac, but you have to hit GetHTML more than once on Windows, and if you hit GetHTML and then GetText, you’ll see what I mean by it returns the value before setting it.

Check out the demo to get a better idea of what I’m trying to do.
HTML Edit Demo.xojo_binary_project

[quote=160187:@Tim Parnell]I do, however there’s so little to work with because HTMLView cannot return the value of a JavaScript.

No. Windows returns the value of the Xojo Property before the HTMLViewer completes the execution of the JavaScript that results in the StatusChange event which is supposed to update that property. The Renderer is already set to WebKit because I don’t trust IE to handle even simple things like html5 properly.

I can’t verify what it’s supposed to be, because the value changes and can be anything at all.
The small delay is exactly what I’d like to accomplish in Xojo so that the HTMLView has a chance to execute the javascript.

I’m working on an object that one can just drop in and use very similar to TextArea. It returns HTML and has much of the functionality of rich text from TextArea. It works perfectly on Mac, but you have to hit GetHTML more than once on Windows, and if you hit GetHTML and then GetText, you’ll see what I mean by it returns the value before setting it.

Check out the demo to get a better idea of what I’m trying to do.
HTML Edit Demo.xojo_binary_project[/quote]

I do not see any ExecuteJavaScript in your project as apparently everything is in an encrypted class so it is not possible to test the particular issue you report and find a workaround.

Since your HTMLEdit class is a black box in which there is probably quite a bit of code, I understand you may not want to share it’s content, but just clicking buttons won’t help much.

Would it not be better to post a small project that demonstrates the bug ?

BTW, would not a timer suffice for a small delay ?

To see the bug, run click getHTML - note how nothing gets returned. Change the value of whatever you’ve got in the rich text editor, and click getHTML again. You’ll get the value of the previous time you clicked getHTML.

The issue is that windows gets to the return myHTMLStatusProperty part of the function before the HTMLViewer actually does the StatusChanged event. I need to make sure StatusChanged happens first.

Did you try it with WebKit?

Yes, I’ve been working with WebKit from the start since I don’t trust IE to handle anything properly.

You’re all going to shun me, but I fixed it using DoEvents. It gives the HTMLViewer a chance to respond.
I was really hoping for a better way to do it, and as a programmer I hate knowing that it could cause instability.
But as a guy who’s been stuck for a while, I’m so glad it works.

I might look into declares for getting JavaScript to return a value directly if there’s outside interest in the control. I’ve got some documentation to write, but it should be available in a few days :slight_smile:

You get the previous value as the Windows WebKit is not threaded like Linux and OS X (they might be threaded either, but they at least update when the ExecuteJavascript is called and not after the method is completed)… Perhaps a feedback request to have “DoEvents” triggered after doing an ExecuteJavascript on Windows could allow Xojo to do it properly instead of the looked down upon manually calling the DoEvents…