Delphi XE5 Android app has to handle time consuming tasks within a thread?

I am just new to android app development from Delphi XE5.

During some time-consuming task being handled in the main process, touching the screen (continuously tapping the screen over and over again) causes the app to end abnormally.

I am assuming that this is caused by the so-called "Application do not respond" and confirm my guess with the block below.

procedure TForm1.Button1Click(Sender: TObject);
begin
    Button1.Text := 'Start';    // Text is 'Button1' on design time
    sleep(10000);
    Button1.Text := 'OK';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    Button2.Text := 'Start';    // Text is 'Button2' on design time
    TThread.CreateAnonymousThread(
        procedure()
        begin
            Sleep(10000);
            TThread.Synchronize(TThread.CurrentThread,
            procedure
            begin
                Button2.Text := 'OK';
            end);
        end).Start;
end;

      

In the case of button 1, continuous taps cause ANR. When I restore the app from the app, the Button1 text shows "Button1". It looks like the Button1Click process has been rewound. In contrast, in the case of Button2, continuous tabs will not trigger ANR.

I have never used threads in Windows application development. Is this the usual way of handling time-consuming tasks on a thread (rather than on the main thread)? Or are there other workarounds?

+3


source to share


2 answers


Threading is the right solution to this problem. The main thread must be responsive if you want to avoid the system detecting that your application is not responding. This is true for mobile platforms as well as desktop platforms.



So, move all long running tasks to threads and hence save your main thread.

+4


source


Don't know much about Delphi. But in native android we use background threads or services to do long tasks so that it doesn't block the UI thread.

For Delphi follow this link. http://www.fmxexpress.com/build-responsive-apps-with-timers-and-threads-using-delphi-xe6-firemonkey-on-android-and-ios/



Hop it will help you.

+1


source







All Articles