Asynchronous Task
Executing background tasks in order to not lock the Main Thread.
Last updated
Was this helpful?
Executing background tasks in order to not lock the Main Thread.
Last updated
Was this helpful?
In order to execute task that can take a few seconds to be completely executed, one must run it apart from the main thread, i.e, the thread responsible for painting components. Executing this kind of task in the main thread may cause a non-good user experience, once the application user may be unable to use and see any progress while the task is being executed.
To avoid such an obstacle, TotalCross provides AsyncTask class, which is a helper to execute task in an asynchronous way. By using AsyncTask, the user can easily execute asynchronous task without complex manipulation of threads and, consequently, not locking the main thread. See the example bellow to learn how to use it.
When the file is completely downloaded, the function onPostExecute is called removing the UpdateListener and reenabling the button dldButton.
Once method execute is called, before executing the asynchronous task, onPreExecute method is also called adding an UpdateListener to update the component in the adequate time interval trough the variable progress. The button dldButton is disabled to avoid user execute the same task many times unnecessarily.