When I use Sleep together with async messagebox (in a class module) the code is crashing. Removing Sleep(0) from the loop works and the messagebox is shown, but the progressdialog is not shown. Is there any way to use both together?
B4X:
Public Sub Import(ImportFile As String) As ResumableSub
ProgressDialogShow2("Importing...", False)
For j = 0 To ImportTable.RowCount - 1
'Prepare source data code
Sleep(0)
Next
ProgressDialogHide
Msgbox2Async("Do you want to import %1 rows?", "Import", "Yes", "", "No", Null, True) '< Crash when using Sleep(0) in the loop
Wait For Msgbox_Result (MsgResult As Int)
If MsgResult = DialogResponse.POSITIVE Then
'...
End If
End Sub
Crash Log:
B4X:
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to read from field 'java.lang.ref.WeakReference anywheresoftware.b4a.BA$SharedProcessBA.activityBA' on a null object reference
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1706)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.ref.WeakReference anywheresoftware.b4a.BA$SharedProcessBA.activityBA' on a null object reference
at anywheresoftware.b4a.keywords.Common.Msgbox2Async(Common.java:486)
at com.divinglog.divelog.import$ResumableSub_Import.resume(import.java:361)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1704)
... 7 more
New video tutorial: Resumable subs is a new feature added in B4J v5.50 / B4i v4.00 / B4A v7.00. It dramatically simplifies the handling of asynchronous tasks. (This feature is a variant of stackless coroutines.) The special feature of resumable subs is that they can be paused, without...
Remember that that a call to Sleep or Wait For in a resumable sub causes the code flow to return to the parent. Example: Sub Button1_Click Sum(1, 2) Log("after sum") End Sub Sub Sum(a As Int, b As Int) Sleep(100) 'this will cause the code flow to return to the parent Log(a + b) End Sub...
No problem, I've attached a small project. Thank you Erel!
Edit: OK, I found out that the code is working directly in the Activity module. If it is in a class module, it crashes. So I think the activity reference is lost because of the Sleep() function and the messagebox cannot be displayed.
Hi, I want to do some Jobs on start of an App. Since there are user interactions and I have to handle with MsgBoxAsync and Wait For, there are Services to start and so on, it's quite difficult to keep the order of the jobs. Especially because anything runs in Activity_Create, and some jobs...