Bug? Wait For with JavaObject

stevel05

Expert
Licensed User
Longtime User
In the attached project I am attempting to recreate an issue that I have found using Wait For with listeners created using JavaObject.

I created the setlistener sub as putting the code in the initialize sub seems to create issues with return values (Which I can understand, and seems best to avoid)

Debug Mode

If you run the app, the listener is called once, then the error:

java.lang.Exception: Sub textchanged_event signature does not match expected signature.

is thrown. (Unless the value happens to be an exit value on the first attempt)

If you uncomment the Sub with the same name, the app runs, but the Wait For callback is only called once (and the actual Sub is not called afterwards for the same event).


Release Mode

If you run the app, the listener is called once, but no error is thrown and the process times out. (Unless the value happens to be an exit value on the first attempt)

If you uncomment the Sub with the same name, the app runs, but the Wait For callback is only called once (and the actual Sub is not called afterwards for the same event).



I'm not sure this is a bug, maybe it needs to be set up differently.
 

Attachments

  • SPT.zip
    1.7 KB · Views: 282

Erel

B4X founder
Staff member
Licensed User
Longtime User
Wait For waits for a single event. Once the event is raised the program continues from that point. It will not wait for another event unless Wait For is called again.

The debugger issue is related to the synchronous event. The solution is to use JO.CreateEventFromUI instead of JO.CreateEvent (CreateEventFromUI should be the default one to use).
Note that it is also better to use CallSubDelayed instead of CallSub.

I see the issue with the Initialize method. I will add a compiler check for this.

Corrected code:
B4X:
'class
Public Sub Initialize(Module As Object,EventName As String)
   JO.InitializeNewInstance("javafx.beans.property.SimpleObjectProperty",Array(Me,"value"))
   mModule = Module
   mEventName = EventName
   SetLisener
End Sub

Private Sub SetLisener
   Listener = JO.CreateEventFromUI("javafx.beans.InvalidationListener","TextChanged","")
   JO.RunMethod("addListener",Array(Listener))
   Do While True
     Wait For TextChanged_Event(MethodName As String,Args() As Object)
     CallSubDelayed3(mModule,mEventName & "_Event","WaitFor",getValue)
   Loop
End Sub
B4X:
'main
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   S.Initialize(Me,"ObjectChanged")
   Dim T As Long = DateTime.Now
   Do While True
     S.Value = Rnd(0,100)
     Wait For (S) ObjectChanged_Event(Source As String,NewValue As Object)
     Log(S.Value)
     If NewValue > 90 Then
       Log("Achieved " & NewValue)
       Exit
     End If
     If DateTime.Now > T + 5000 Then
       Log("TimeOut")
       Exit
     End If
   Loop
End Sub
 

stevel05

Expert
Licensed User
Longtime User
Thanks very much Erel, I will let that sink in a while to get it straight in my mind.
 
Top