简体   繁体   中英

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

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM