In many cases you need to run a task in a few seconds. The solution for such cases is to create a timer and then execute the task in the timer's Tick event. This is good for a single task. However if you need to run multiple tasks then it becomes difficult to maintain.
This small class makes it much simpler. You just need to call CallSubPlus with the target sub and the delay.
Internally it uses a (one shot) timer together with CallSubDelayed or CallSub.
There are four methods:
CallSubPlus, CallSubPlus2, CallSubDelayedPlus and CallSubDelayedPlus2.
CallSubPlus and CallSubDelayedPlus should be used with subs with no parameters. The others should be used with subs with a single parameter. The parameter type must be an array of objects.
For example:
csu is declared in the Starter service:
Notes
- Make sure to include an underscore in the target subs names if you intend to compile with obfuscation.
- CallSubDelayed will start the target service or activity if needed (see this tutorial for more information: https://www.b4x.com/android/forum/t...etween-activities-and-services.18691/#content).
- You can safely call multiple subs. This can be useful to break a long task into smaller tasks.
- This class can also be used with B4i and B4J.
- Add this class to your project with Project - Add Existing Module.
This small class makes it much simpler. You just need to call CallSubPlus with the target sub and the delay.
Internally it uses a (one shot) timer together with CallSubDelayed or CallSub.
There are four methods:
CallSubPlus, CallSubPlus2, CallSubDelayedPlus and CallSubDelayedPlus2.
CallSubPlus and CallSubDelayedPlus should be used with subs with no parameters. The others should be used with subs with a single parameter. The parameter type must be an array of objects.
For example:
B4X:
Sub Activity_Click
Log($"Click: $Time{DateTime.Now}"$)
Starter.csu.CallSubPlus(Me, "Sub_1", 2000) 'run in two seconds
Starter.csu.CallSubPlus2(Me, "Sub_2", 1000, Array("Value 1", "Value 2")) 'run in one second
End Sub
Sub Sub_1
Log($"Sub_1: $Time{DateTime.Now}"$)
End Sub
Sub Sub_2(args() As Object)
Log($"Sub_2: $Time{DateTime.Now}, arg(0)=${args(0)}"$)
End Sub
csu is declared in the Starter service:
B4X:
Sub Process_Globals
Public csu As CallSubUtils
End Sub
Sub Service_Create
csu.Initialize
End Sub
Notes
- Make sure to include an underscore in the target subs names if you intend to compile with obfuscation.
- CallSubDelayed will start the target service or activity if needed (see this tutorial for more information: https://www.b4x.com/android/forum/t...etween-activities-and-services.18691/#content).
- You can safely call multiple subs. This can be useful to break a long task into smaller tasks.
- This class can also be used with B4i and B4J.
- Add this class to your project with Project - Add Existing Module.