B4J Code Snippet Pause execution in Console app


B4X:
Sub AppStart (Args() As String)
    sys.InitializeStatic("java.lang.System")
    Dim user_input As String
    Log("Press <q> to exit..")
    Do Until user_input.ToLowerCase = "q"
        user_input = Input("> ")
       
        If user_input.EqualsIgnoreCase("hack") Then
            Log("root@kali$ Hacking your B4X forum password. Please wait...")
            Pause(2000)

            Log("root@kali$ Processing... (10%)")
            Pause(1000)
            Log("root@kali$ Processing... (20%)")
            Pause(2000)
            Log("root@kali$ Processing... (50%)")
            Pause(500)
            Log("root@kali$ Processing... (80%)")
            Pause(500)
            Log("root@kali$ Processing... (90%)")
            Pause(500)
           
            Log("root@kali$ Completed. Your password is (just kidding)")
            Pause(1000)
        Else
            Log($"You entered '${user_input}'"$)
        End If
    Loop
    Log("Goodbye")
    Delay(500)
    StartMessageLoop
End Sub

B4X:
Sub Pause (Milliseconds As Long)
    Private jo As JavaObject
    jo.InitializeStatic("java.lang.Thread")
    jo.RunMethod("sleep", Array As Object(Milliseconds))
End Sub

GitHub: https://github.com/pyhoon/console-read-input-b4j
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
BTW, you can use the regular and in most cases better Sleep method.

B4X:
Sub AppStart (Args() As String)
    sys.InitializeStatic("java.lang.System")
    Start
   StartMessageLoop
End Sub

Sub Start
    Dim user_input As String
    Log("Press <q> to exit..")
    Do Until user_input.ToLowerCase = "q"
        user_input = Input("> ")
       
        If user_input.EqualsIgnoreCase("hack") Then
            Log("root@kali$ Hacking your B4X forum password. Please wait...")
            Sleep(2000)

            Log("root@kali$ Processing... (10%)")
            Sleep(1000)
            Log("root@kali$ Processing... (20%)")
            Sleep(2000)
            Log("root@kali$ Processing... (50%)")
            Sleep(500)
            Log("root@kali$ Processing... (80%)")
            Sleep(500)
            Log("root@kali$ Processing... (90%)")
            Sleep(500)
           
            Log("root@kali$ Completed. Your password is (just kidding)")
            Sleep(1000)
        Else
            Log($"You entered '${user_input}'"$)
        End If
    Loop
    Log("Goodbye")
    Sleep(500)
End Sub

This will allow a real app to do many more things while waiting.
Post about resumable subs in non-ui apps: https://www.b4x.com/android/forum/threads/resumablesub-in-non-ui-app.106663/#post-667569
 

aeric

Expert
Licensed User
Longtime User
BTW, you can use the regular and in most cases better Sleep method.

B4X:
Sub AppStart (Args() As String)
    sys.InitializeStatic("java.lang.System")
    Start
   StartMessageLoop
End Sub

Sub Start
    Dim user_input As String
    Log("Press <q> to exit..")
    Do Until user_input.ToLowerCase = "q"
        user_input = Input("> ")
      
        If user_input.EqualsIgnoreCase("hack") Then
            Log("root@kali$ Hacking your B4X forum password. Please wait...")
            Sleep(2000)

            Log("root@kali$ Processing... (10%)")
            Sleep(1000)
            Log("root@kali$ Processing... (20%)")
            Sleep(2000)
            Log("root@kali$ Processing... (50%)")
            Sleep(500)
            Log("root@kali$ Processing... (80%)")
            Sleep(500)
            Log("root@kali$ Processing... (90%)")
            Sleep(500)
          
            Log("root@kali$ Completed. Your password is (just kidding)")
            Sleep(1000)
        Else
            Log($"You entered '${user_input}'"$)
        End If
    Loop
    Log("Goodbye")
    Sleep(500)
End Sub

This will allow a real app to do many more things while waiting.
Post about resumable subs in non-ui apps: https://www.b4x.com/android/forum/threads/resumablesub-in-non-ui-app.106663/#post-667569
Thanks Erel. It works but I need to add StopMessageLoop before the last End Sub.
 
Top