Android Question Sleep and resumable subs undestanding

hookshy

Well-Known Member
Licensed User
Longtime User
I have receive some crash lately using this code

B4X:
Sub grab_customer
    Sleep(2000) 
If manager_ready Then
    'grab customer from time to time
    If File.Exists(File.DirInternal,"trialcustomer.txt")=False Then
        File.WriteString(File.DirInternal,"trialcustomer.txt",DateTime.Now)
                            
    Else
                    
        trialcustomer=File.ReadString(File.DirInternal,"trialcustomer.txt")
                
        If  trialcustomer < DateTime.Now - 3*DateTime.TicksPerDay Then
        
            Dim re As Int
            re=Msgbox2("We thank you for choosing our apps. Do you want to unlock star delta features now ?","Electrician app","OK LET'S GO","no","",Null)
            If re=DialogResponse.POSITIVE Then
                 Try
                    manager.RequestPayment("noads","inapp","ads")
                Catch
                    ToastMessageShow("Billing manager not ready",True)
                End Try

            Else if re=DialogResponse.NEGATIVE Then
                trialcustomer=DateTime.Now 'reset and ask after 3 days again
                File.WriteString(File.DirInternal,"trialcustomer.txt",trialcustomer)
                
                
            Else
                'same practice
                trialcustomer=DateTime.Now
                File.WriteString(File.DirInternal,"trialcustomer.txt",trialcustomer)
            
            
            End If
                
        End If

    End If

End If

End Sub


First of all I guess I am doing something wrong and please do correct my implementation !
I just wanted to wait for the app splash to load before showing something else, I guess is better to use a timer instead.

Please explain why is important for the main thread to keep on going .
I do not want to start changing all msgbox2 to the new asynchronous method because I do not understand it.
You say that Msgbox2 will crash our apps and is not wanted any more ?
I am very confused about the sleep usage and now with the msgbox as well !!
 

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
B4X:
Sub close_intro
    
    Sleep(5000)
    pnl_intro.Left=Activity.Width
        
End Sub

Is this sub corect ? ....I do not want to return or wait for nothing ...just want to postpone an action
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Because the O.S. can consider your app blocked and could kill it.
With the activity on front ? ...what if everyting that will run after the msgbox depends on this result ?
I can keep the msgbox2 on screen forever ...the app does not crash once openned
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Here is how the error looks like for the code from first post
B4X:
anywheresoftware.b4a.B4AUncaughtException:
  at anywheresoftware.b4a.Msgbox.msgbox (Msgbox.java:172)
  at anywheresoftware.b4a.keywords.Common.Msgbox2 (Common.java:443)
  at hs.electrician.com.main$ResumableSub_grab_customer.resume (main.java:1298)
  at anywheresoftware.b4a.keywords.Common$13.run (Common.java:1680)
  at android.os.Handler.handleCallback (Handler.java:790)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6543)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:440)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:810)

Could somebody explained why the app crashed ?
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
i use sleep in many subs without returning a value .... it is important to understand If this code will block the app or not
B4X:
Sub close_intro
  
    Sleep(5000)
    pnl_intro.Left=Activity.Width
      
End Sub

to fix the problem with the code from post 1 , I let the code run without the msgbox2 ...I bring a panel in front instead
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Here is another example aplication from my apps....i fixed the keyboard close issue by adding a sleep(50) in order to the thread to be able to process this request
B4X:
Sub hide_keyboard
    Sleep(50)
    IME.HideKeyboard
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
i use sleep in many subs without returning a value .... it is important to understand If this code will block the app or not
B4X:
Sub close_intro
    Sleep(5000)
    pnl_intro.Left=Activity.Width
End Sub

I'm not free, now, I answer not so perfectly.

No, that code will not block your app; is works, as Erel said many times, this way:

You can consider Sleep (and Wait For) as it was a Return - exit routine - but after those 5000ms the process - code flow, I don't know how to explain better this, will return to the next line, pnl_intro...

I hope it is... "clear" :(
 
Upvote 0
Top