Android Question [B4A] [SOLVED] Button_click event forward to class with callsub; class wait for event; Release mode

mading1309

Member
Licensed User
I encountered the following situation. I don't know where is my problem.
I created a test project because I expected one of my other subroutines influence the application and result in the described behaviour
With the test project I found the reason in a sleep(10) instruction

A brief description of the main activity:
Two buttons, button1 and button2, using the same Click Event "Button_click" added with the designer
The Button_Click event fires an event "clicked" which is "sampled" through a wait for instruction in the class objllk
Two "callback" functions of the class objllk. One changes the color of the two buttons, the other one changes the text attribute of button1

A brief description of the class objllk
This class just waits for the forwarded click event of the main activity through the event "clicked"
Each event changes an internal counter form 0 to 2 for the inner loop
Every completed innerloop changes the color of the buttons and shows the value of the counter of the outer loop
Change the color and show the value are two "callback" functions addressed to active instance of the class objllk

When I compile the sample project in DEBUG mode the test project works as I expected.
If I compile the sample project in RELEASE mode with the SLEEP (10) instruction in the subroutine test of the class objllk then the program does not what I expect.
After the SLEEP(10) instruction the event "clicked", fired by the button_click event of the main activity, does not continue the program flow after the wait for instruction of the subroutine test of the class objllk.
If I compile the project in RELEASE mode without the SLEEP(10) instruction in the subroutine test the class objllk then the program runs as I expect

I attached the project with the logs of the small programm as comments in the subroutine test of the class objllk

Just for reference the subroutine test of the class objllk


Code:
Sub test
    Dim NumOfClick As Byte
    Dim NumofHints As Long
    Do While hint(NumofHints) =5
        NumofHints=NumofHints+1
        NumOfClick=0
        Do Until NumOfClick=2
            Log ("wait for Click event")
            wait for clicked
            NumOfClick=NumOfClick+1
            If NumOfClick=2 Then show (NumofHints)
            Log ("After click event " & NumOfClick & " NumofHints  " & (NumofHints Mod 3))
        Loop
'********************************
'????????????????????????????????
'In DEBUG Mode the subroutine test runs as I exect
'It seems that the sleep(10) instruction in line 72 is the reason why the subroutine
'does not function in RELEASE mode compare to DEBUG Mode.
'In RELEASE mode for compilation with activated sleep(10) instruction the subroutine test does not what I expect
'The event of the wait for instruction is not fired again after excecuting the sleep instruction
'The log screen
'        ** Activity (Main) Pause, UserClosed = False **
'        *** Service (Starter) Create ***
'        ** Service (Starter) Start **
'        ** Activity (Main) Create, isFirst = True **
'        ** Activity (Main) Resume **
'        wait for Click event
'        Button_click
'        After click event 1 NumofHints  1
'        wait for Click event
'        Button_click
'        Button value 1
'        After click event 2 NumofHints  1
'        wait for Click event
'        Button_click
'        Button_click
'        Button_click
'        Button_click
'        Button_click

'In DEBUG and RELEASE mode the subroutine works
'with deactivated sleep(10) instruction  as I exspect
'the log screen
'        --------- beginning of system
'        *** Service (Starter) Create ***
'        ** Service (Starter) Start **
'        ** Activity (Main) Create, isFirst = True **
'        ** Activity (Main) Resume **
'        wait for Click event
'        Button_click
'        After click event 1 NumofHints  1
'        wait for Click event
'        Button_click
'        Button value 1
'        After click event 2 NumofHints  1
'        wait for Click event
'        Button_click
'        After click event 1 NumofHints  2
'        wait for Click event
'        Button_click
'        Button value 2
'        After click event 2 NumofHints  2
'        wait for Click event
'        Button_click
'        After click event 1 NumofHints  0
'        wait for Click event
'        Button_click
'        Button value 3
'        After click event 2 NumofHints  0
'
'What is my problem? What I am doing wrong?
        Sleep(10)
    Loop
End Sub
Where is my problem? Do I understand the idea of RESUMABLE subs and WAIT FOR {event} and SLEEP still wrong?
My skill of JAVA is to low to fully understand and following the details of the generated JAVA Source files to find out my problem.

Any help , go around and explanation is welcome

Mading
 

Attachments

Last edited:

Erel

Administrator
Staff member
Licensed User
My skill of JAVA is to low to fully understand and following the details of the generated JAVA Source files to find out my problem.
How is Java related here? Resumable subs have nothing to do with Java.

1. It is a mistake to initialize objects of a different module. Better to declare llk in Main module.
2. Your code with Sleep cannot work. The "clicked" event will be lost in any case where the sub is sleeping. It will only be caused if the sub was paused because of the wait for line.
3. It is better to use CallSubDelayed with the clicked event.
 

mading1309

Member
Licensed User
Sorry for my late reply. Since the Corona Quarantine more often the internet performance is weak.

@Erel Thanks your hints. I followed them and could solve my questions.
Am I understand right, that SLEEP has a higher priority than WAIT FOR because the event can't trigger the WAIT FOR as long SLEEP is activ?
I expect with higher JAVA skills it should be not a big problem to find answers to my questions.

Thanks your quick support and great job with these B4X tools
 

Erel

Administrator
Staff member
Licensed User
Am I understand right, that SLEEP has a higher priority than WAIT FOR because the event can't trigger the WAIT FOR as long SLEEP is activ?
No.
It just happens that when you raise the "clicked" event the event is not caught because the "resumable sub" is sleeping and not waiting for the event.
I expect with higher JAVA skills it should be not a big problem to find answers to my questions.
Expectations are good however Java has nothing to do with it. I will write again, this is a special feature of B4X.
 
Top