Recent

Author Topic: [LAMW] TThread Synchronize and Queue not working  (Read 8270 times)

WayneSherman

  • Full Member
  • ***
  • Posts: 243
[LAMW] TThread Synchronize and Queue not working
« on: June 07, 2018, 08:28:16 pm »
TThread is working to create and execute a thread, but TThread.Synchronize and TThread.Queue are not working.

In Android/LAMW, from my secondary thread how can I cause the main GUI thread to execute a method or procedure?

Does SendMessage / PostMessage work on Android?

Thanks

« Last Edit: June 07, 2018, 08:31:06 pm by WayneSherman »

WayneSherman

  • Full Member
  • ***
  • Posts: 243
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #1 on: June 12, 2018, 04:40:04 am »
Until we have a better way I used a JTimer as a work around.  The jTimer.OnTimer event runs in the context of the main thread.  I use it to poll a boolean variable that indicates the worker thread has some data waiting to update the GUI.

A proper solution may require the Native_Activity input queue (see onInputQueueCreated) and possibly Looper.

Some discussion is here:  http://www.ikerhurtado.com/android-ndk-native-activity-app-glue-lib-lifecycle-threads




« Last Edit: June 12, 2018, 04:41:37 am by WayneSherman »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #2 on: June 15, 2018, 03:17:55 pm »
I currently don't know the internals of LAMW, but for TThread.Synchronize() and TThread.Queue() to work the main thread needs to call CheckSynchronize().

wadman

  • New Member
  • *
  • Posts: 37
    • wadman's home
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #3 on: June 15, 2018, 03:33:01 pm »

bmcsoft

  • New Member
  • *
  • Posts: 21
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #4 on: December 06, 2020, 02:40:40 pm »
I currently don't know the internals of LAMW, but for TThread.Synchronize() and TThread.Queue() to work the main thread needs to call CheckSynchronize().
Yes, calling the CheckSynchronize from the main thread makes to handle the request (synchronize calls in the synchronize queue). But how to make it frequently?
I want to make UDP-chat. Incoming messages received randomly and frequently. Do I need jTimer anyway? What a cumbersome decision!

WayneSherman

  • Full Member
  • ***
  • Posts: 243
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #5 on: December 06, 2020, 05:22:53 pm »
  Do I need jTimer anyway? What a cumbersome decision!
I just looked at this again and found the java method "activity.runOnUiThread":

https://stackoverflow.com/questions/13991952/messaging-to-ui-thread

https://developer.android.com/reference/android/app/Activity#runOnUiThread%28java.lang.Runnable%29

Currently, in the LAMW framework, runOnUiThread appears to only be used in one place (called from java):
https://github.com/jmpessoa/lazandroidmodulewizard/blob/eb262aaec6cb6abb9f5cedb9cd8e1f4a9205e208/android_wizard/smartdesigner/java/jBluetoothClientSocket.java#L376

Code: Java  [Select][+][-]
  1. controls.activity.runOnUiThread(new Runnable() {
  2.         public void run() {
  3.                 OnConnectResult();
  4.         };
  5. });
  6.  

I found the Controls.activity java class defined here:
https://github.com/jmpessoa/lazandroidmodulewizard/blob/9fb348aa1819794c47cc464561ef93d287e92a1b/android_wizard/smartdesigner/java/Controls.java#L2179

I think the LAMW framework can be extended to make "runOnUiThread" available to pascal code.  But I don't know how to do it.
« Last Edit: December 06, 2020, 05:47:26 pm by WayneSherman »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #6 on: December 06, 2020, 05:38:48 pm »

Quote
I think the LAMW framework can be extended to make "runOnUiThread" available to pascal code.  But I don't know how to do it.

I will do it for you just now!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

WayneSherman

  • Full Member
  • ***
  • Posts: 243
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #7 on: December 06, 2020, 05:50:36 pm »
Thank you Jose.  :D

Some background to previous message is here:
https://stackoverflow.com/questions/13991952/messaging-to-ui-thread
and here
https://developer.android.com/guide/components/processes-and-threads.html#WorkerThreads

There is also a way to post to the main UI message queue to run code in the main UI thread using the "View" java class (and its descendants):
https://developer.android.com/reference/android/view/View#post(java.lang.Runnable)
https://developer.android.com/reference/android/view/View#postDelayed(java.lang.Runnable,%20long)

The "post" and "postDelayed" methods are also used in the LAMW framework in a few places, but as far as I can tell, they are not exposed in a generic way for pascal code to use.
« Last Edit: December 06, 2020, 05:54:48 pm by WayneSherman »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #8 on: December 06, 2020, 06:44:27 pm »
for the first suggestion:

What about this warapper?

Code: C  [Select][+][-]
  1.         public void RunOnUiThread() {
  2.  
  3.            controls.activity.runOnUiThread(new Runnable() {
  4.                public void run() {
  5.                   controls.pOnRunOnUiThread(PasObj);  //this wil call pascal in jForm  new event "OnRunOnUiThread"
  6.                };
  7.            });
  8.  
  9.        }
  10.  


Done!

jForm: new method RunOnUiThread();
After

Self.RunOnUiThread()

You need handle the event property:

"OnRunOnUiThread"  to put your code....

Can you test this solution?

If need I can try some modification......
« Last Edit: December 06, 2020, 08:27:21 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

WayneSherman

  • Full Member
  • ***
  • Posts: 243
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #9 on: December 06, 2020, 10:17:02 pm »
for the first suggestion:
What about this warapper?
jForm: new method RunOnUiThread();

I think the API can be made more flexible and try to store less or no state in jform.  Also pass some reference / context data so when the method runs in a UI thread it has an object or index that can be used to reference the data it needs.

Designing an API should not be done hastily, let me give this a little thought.

« Last Edit: December 06, 2020, 10:21:48 pm by WayneSherman »

WayneSherman

  • Full Member
  • ***
  • Posts: 243
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #10 on: December 06, 2020, 10:45:34 pm »
It would be nice to allow the API user to define their own methods in any class which get called  by the UI thread:

Code: Pascal  [Select][+][-]
  1. //LAMW Framework Code
  2. //android_bridges/androidwidget.pas
  3. type
  4.   TRunOnUIThreadMethod = procedure(const RefObject: TObject; const RefData: Integer) of Object;
  5.   //or if pointers are ok, then reference data can be passed in the pointer.
  6.   //TRunOnUIThreadMethod = procedure(const RefData: Pointer) of Object;
  7.    
  8.   jForm = class(TAndroidForm)
  9.   public
  10.   ...
  11.     RunOnUiThread(const AMethod: TRunOnUIThreadMethod; const RefObject: TObject; const RefData: Integer);
  12.   ...    
  13.   end;
  14.  
  15. //User Code
  16.   TAndroidModule1 = class(jForm)
  17.   ...
  18.   end;
  19.  
  20.   TSomeClass = class
  21.   ...
  22.     procedure RunThisMethodInTheUIThread(const RefObject: TObject; const RefData: Integer);
  23.   ...
  24.   end;
  25.  
  26. var
  27.   AndroidModule1: TAndroidModule1;
  28.   SomeClass: TSomeClass;
  29.  
  30. begin
  31.   AndroidModule1.RunOnUiThread(@SomeClass.RunThisMethodInTheUIThread, nil, 0);
  32.  
« Last Edit: December 06, 2020, 10:55:28 pm by WayneSherman »

WayneSherman

  • Full Member
  • ***
  • Posts: 243
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #11 on: December 06, 2020, 11:43:04 pm »
Is jForm the appropriate class to implement RunOnUiThread?

In java/android RunOnUiThread is a method of the Activity class, and in LAMW "activity" is a member of the Controls class (in Controls.java).

So maybe jControl the right place to implement RunOnUiThread?:

Code: Pascal  [Select][+][-]
  1.   jControl = class(TComponent)
  2.   public
  3.   ...
  4.     RunOnUiThread(const AMethod: TRunOnUIThreadMethod; const RefObject: TObject; const RefData: Integer);
  5.   ...    
  6.   end;
  7.  
  8.  
In that case, any jcontrol can make use of it.

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #12 on: December 07, 2020, 12:20:19 am »
Quote
So maybe jControl the right place to implement RunOnUiThread?

Ok. I will try this solution, too...

PS.

Sorry... but  pascal "jControl"   and "Controls.java" don't have the same semantic....   
"Controls.java" is where we define "jForm" class, too...  more important: all LAMW [java side] component have a reference to
the "Controls"  instance of Controls.java

The name ""Controls.java"" is a legacy code that refactored lost the original semantic [a container to all lamw controls....]
today, each component has its own java class file... but ""Controls.java"" still is the main [JNI] java/pascal interface. 
Maybe we can change it to  "Controller.java"  in some time....
« Last Edit: December 07, 2020, 12:51:41 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #13 on: December 07, 2020, 06:40:56 pm »
I just changed

the jForm method signature to:

RunOnUiThread(tag: integer)

[changed method signature ...]

Use:

Self.RunOnUiThread(1)

then, you need handle the event property:

"OnRunOnUiThread"  to put your code...

side note: Are you using LAMW "jUDPSocket" Component?
[if yes,  can we improved it?]
« Last Edit: December 07, 2020, 07:20:03 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Segator

  • Full Member
  • ***
  • Posts: 168
    • https://github.com/Nenirey
Re: [LAMW] TThread Synchronize and Queue not working
« Reply #14 on: December 08, 2020, 03:20:27 pm »
RunOnUiThread(i) is like an AsyncTask or a pascal thread?, i can execute io operations with out affect the UI?
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

 

TinyPortal © 2005-2018