Hi All,
UPDATED - You can add Parameters (maximum 2) to your task
(You can also add as many variable as you need with declaring them as global variables. So you can use them in your BG sub)
UPDATED 2 - You can return a result to Sub_Done event. See Example below.
------------------------------------------------------------------------------------------------
Run a Sub in Background thread (without blocking the UI).
1. Add the class attached to your project
2.
Example without Parameter
Example with 1 Parameter
Example with 2 Paramters
Example with returning a Result to the main process
ATTENTION !!! Do not forget that you can not change the UI in the Background Sub. You have to Update the UI in Sub_Done event or after.
UPDATED - You can add Parameters (maximum 2) to your task
(You can also add as many variable as you need with declaring them as global variables. So you can use them in your BG sub)
UPDATED 2 - You can return a result to Sub_Done event. See Example below.
------------------------------------------------------------------------------------------------
Run a Sub in Background thread (without blocking the UI).
1. Add the class attached to your project
2.
Example without Parameter
B4X:
Dim myBGTask As BackgroundTask
myBGTask.Initialize("MyTask",Me,Array(Null))
Sub MyTask
'Do your background staff
'NO UI
End Sub
Sub MyTask_Done
'Background Task Finished
'Update your UI
End Sub
Example with 1 Parameter
B4X:
Dim myBGTask As BackgroundTask
myBGTask.Initialize("MyTask",Me,Array(yourParam))
Sub MyTask(Param as string/...)
'Do your background staff
'NO UI
End Sub
Sub MyTask_Done
'Background Task Finished
'Update your UI
End Sub
Example with 2 Paramters
B4X:
Dim myBGTask As BackgroundTask
myBGTask.Initialize("MyTask",Me,Array(Param1, Param2))
Sub MyTask(Param1 as String, Param2 as Float)
'Do your background staff
'NO UI
End Sub
Sub MyTask_Done
'Background Task Finished
'Update your UI
End Sub
Example with returning a Result to the main process
B4X:
Dim myBGTask As BackgroundTask
myBGTask.Initialize("MyTask",Me,Array(Null))
Sub MyTask
'Do your background staff
'NO UI
myBGTask.Result= YourResult
End Sub
Sub MyTask_Done (Result)
'Background Task Finished
'Update your UI
End Sub
ATTENTION !!! Do not forget that you can not change the UI in the Background Sub. You have to Update the UI in Sub_Done event or after.
Attachments
Last edited: