Can threads communicate?

  1. 8 weeks ago

    We're told that threads cannot communicate with the main app but can threads communicate with each other?

  2. Christian S

    Feb 11 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    threads can access properties, even those from main thread or globals.

    Or what kind of communication do you need?

  3. Joost R

    Feb 12 Pre-Release Testers, Xojo Pro The Netherlands

    I would considder creatng a class and have a referece to the object accessable focr all threads.

  4. David C

    Feb 12 Pre-Release Testers, Xojo Pro, XDC Speakers Derby, ITM

    FWIW multiple threads (and apps) can access a multi-user SQLite database as a means of communication of data of any size.

  5. I'm creating a thread to print to a PDF but sometimes calculations will have be performed, called from the print thread. If the calculations need to be a thread also, I'd rather not create some huge thread that involves everything my app does

  6. Norman P

    Feb 12 Pre-Release Testers, Xojo Pro outside enjoying the fresh air

    threads can call global functions and methods in your app and those methods / functions will run in that threads context

  7. Ulrich B

    Feb 12 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...

    @John S We're told that threads cannot communicate with the main app but can threads communicate with each other?

    Just to clarify this: A thread must not access a GUI element. It can otherwise communicate fully with the app, while you should be aware that as Norman said method calls from a thread will inherit the thread context.
    For these tasks, thread has the new UserInterfaceUpdate event.

  8. Thanks Ulrich. My calculations write to a database rather than the user interface so I should be good to go . . . Anyone disagree?

  9. Ulrich B

    Feb 12 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...

    Not me. Also not David if you read his reply ;)

  10. Bob K

    Feb 12 Pre-Release Testers, Xojo Pro Kansas City

    @John S Thanks Ulrich. My calculations write to a database rather than the user interface so I should be good to go . . . Anyone disagree?

    Nope. As long as you don't touch a UI element (this means even a property of control) you're good to go.

  11. Michel B

    Feb 12 Pre-Release Testers, Xojo Pro

    @John S We're told that threads cannot communicate with the main app but can threads communicate with each other?

    Desktop threads cannot access the UI.

    But that is not true of Web threads, which can access the UI, and communicate with each other.

    @John S I'm creating a thread to print to a PDF but sometimes calculations will have be performed, called from the print thread. If the calculations need to be a thread also, I'd rather not create some huge thread that involves everything my app does

    You could start the print thread from the one that computes, when it is completed.

    Or you could set a flag, a boolean property, when calculation is complete, and have a timer check every second or so that it is set to true, to start printing.

  12. 7 weeks ago

    Joost R

    Feb 12 Pre-Release Testers, Xojo Pro The Netherlands

    Still, creating a singleton object for the communication between treads is my best option and in line with the OOP principle.

  13. Does cannot access a property of a UI element include containers?

  14. Bob K

    Feb 13 Pre-Release Testers, Xojo Pro Kansas City

    @John S Does cannot access a property of a UI element include containers?

    Depends on what you're accessing in the container. If it's a property defined by you you're okay. If it's a property of a UI control then no.

    The way I get around this in classic API is using a variant of the Task Thread which fires a timer (if you recall, timer.action event fires ONLY on the main thread) and then I can pass whatever information back to the UI.

    In the API 2 there's a new event on threads called UserInterfaceUpdate that is doing the same thing for you but it's built-in. Frankly, one of the better enhancements in API 2.

  15. Brian O

    Feb 13 Pre-Release Testers Calgary, AB

    I recommend having a message queue class.
    The queue will contain messages that a thread or timer (main thread) can access to send a message or d-queue a message.

  16. Michel B

    Feb 13 Pre-Release Testers, Xojo Pro

    @Bob K Depends on what you're accessing in the container. If it's a property defined by you you're okay. If it's a property of a UI control then no.

    The way I get around this in classic API is using a variant of the Task Thread which fires a timer (if you recall, timer.action event fires ONLY on the main thread) and then I can pass whatever information back to the UI.

    In the API 2 there's a new event on threads called UserInterfaceUpdate that is doing the same thing for you but it's built-in. Frankly, one of the better enhancements in API 2.

    The question was posted in the Web channel. What you describe is for Desktop.

    Web threads can access the UI.

  17. Michel B

    Feb 13 Pre-Release Testers, Xojo Pro

    Here is a small project demonstrating how WebThreads access directly a Web project UI.

    WebThreadAccessingUI.xojo_binary_project

  18. Christian S

    Feb 13 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    For web projects, the UI access is not a big problem.
    But you may need a WebSessionContext object for session access.

  19. Michel B

    Feb 13 Pre-Release Testers, Xojo Pro

    Indeed. Session is not accessible in some cases.

    For instance, no session in server side Timers, as opposed to WebTimers (what you get as default).

    Or no session in HandleURL.

or Sign Up to reply!