Delphi XE5 Android app should process time-consuming tasks inside a thread?

2.2k views Asked by At

I'm just a newbie to the Android application development by Delphi XE5.

During some time-consuming task being processed in the main process, pelt of tapping on the screen (continuously tap the screen over and over again) cause the application's abnormal end.

I guess it is because of so called 'Application Not Responding' and confirm my guess by the code 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 the Button1, continuous taps cause ANR. When I restore the App from app stack, the text of Button1 shows 'Button1'. It looks like the process of Button1Click was rewound. On the contrary, in the case of the Button2, continuous tabs won't cause ANR.

I've never used threads in the development of Windows applications. Is it normal way of processing time-consuming tasks in a thread (not in main thread)? Or are there other workarounds?

2

There are 2 answers

0
David Heffernan On BEST ANSWER

Threading is the correct solution to this problem. The main thread needs to be responsive if you wish to avoid the system determining that your application is unresponsive. That is just as true for mobile platforms as it is for desktop platforms.

So, move all long running tasks onto threads, and so keep your main thread responsive.

0
Nitesh On

Don't know much about Delphi. But in native android we use Background threads or services to perform Long running tasks so that it doesn't block the UI thread.

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

Hop it helps you.