I have tracked down the conditions when jAsyncTask crashes.
https://github.com/jmpessoa/lazandroidmodulewizard/tree/master/demos/GUI/AppAsyncTaskDemo1function TAndroidModule1.DoTask(Progress: integer): boolean;
begin
Result:= True; //doing task ...
if Progress < 100 then
begin
Sleep(100 - Progress); //processing task...
end
else
Result:= False; //done !!
end;
procedure TAndroidModule1.jAsyncTask1ProgressUpdate(Sender: TObject; progress: integer; out progressUpdate: integer);
begin
if progress <= jProgressBar1.Max then
begin
jProgressBar1.Progress:= progress;
// jDialogProgress1.Msg:= 'Lamw: Please, wait... ['+IntToStr(progress)+']';
progressUpdate:= progress + 1; //out param !
end
else
begin
jProgressBar1.Progress:= 0;
//jDialogProgress1.Msg:= 'Lamw: Please, wait...';
progressUpdate:= 0; //out param !
end;
end;
AppAsyncTaskDemo1 has two functions:
1) function TAndroidModule1.DoTask(Progress: integer): boolean;
2) procedure TAndroidModule1.jAsyncTask1ProgressUpdate(Sender: TObject; progress: integer; out progressUpdate: integer);
These two functions (
DoTask and
jAsyncTask1ProgressUpdate) run simultaneously in parallel.
These two functions have different times (of processing calculations), and one of them completes calculations earlier than the other.
In case when
DoTask takes longer time than
jAsyncTask1ProgressUpdate, then AppAsyncTaskDemo1 runs ok.
However if
DoTask finishes faster than
jAsyncTask1ProgressUpdate, then
AppAsyncTaskDemo1 crashes.
As for example, when
jAsyncTask1ProgressUpdate updates some GUI component (for example the
jProgressBar1), it take quite a long time, and in case if
DoTask contains simple short calculations,
DoTask finishes faster than it takes to update
jProgressBar1, and the app crashes.
As a workaround solution I use the following – I have added separate
jTimer which run in separate thread and
jTimer updates
jProgressBar1.
In order to avoid crash do not update
jProgressBar1 in
jAsyncTask1ProgressUpdate, because this will lead to crash in case when
DoTask finishes faster that
jAsyncTask1ProgressUpdate.