Application isn't responding - Delphi XE6 - Android

六眼飞鱼酱① 提交于 2019-12-13 16:32:43

问题


On Android, when I touch the screen during a long function (required time > 30 sec) 15sec after the click, a message "Application isn't responding" appears. I don't want to see it.

I did a test project, with 2 buttons and a function "LongProcess" for simulate a long process (it's just a sleep of 30 sec). My first button "LaunchFunction" just call the function. My second button "LaunchThread", launch a Thread who will execute my "LongProcess". In the first case I have my problem but in the second case it works perfectly (the message will never appears because my main form is not waiting).

However, I have to wait the end of "LongProcess" (therefore the end of the Thread) because I have to do others things after it who need the result of the "LongProcess". So I tried to wait my Thread with many methods. I tried with the "WaitFor" of TThread Class but it repeat the initial problem. I tried also with a simple "while".

while not fThread.Finished do
begin
  Sleep(500);
end;

But it's the same, if i touch the screen the popup will appears again. Critical Section instead of the "while" or the "Thread.WaitFor" did exactly the same.

So I tried to update the GUI in my "while" for show to Android that the application is working.

while not fThread.Finished do
begin
  Sleep(100);
  Label_Test.Text := 'Msg' + IntToStr(i);
  Inc(i);
  Application.ProcessMessages;
end; 

I see my label value change, if I touch the screen nothings change. 15sec later I will have the popup (but I will still see my label be updated on the background).

Someone have an idea ? Maybe can we disable event during a long process (so the click will not be in the queue so he should not be considered like "not responding" after 15 sec). Application.ProcessMessage don't seems works about that on Android. Or maybe something in the Android API exists for say to the OS we are not inactive ?

For finish, if I click on "Wait" the application will work perfectly. If I don't touch the screen also (w/o thread too, until I don't touch the screen), but I see so many user click on "OK" like a robot (this close the application ofc...). Ty for your futur help

ps : I tried to replace the thread by a timer because I saw it on a forum, but it changed nothings.

ps2 : Here a .zip or the demo project http://www.partage-facile.com/YOJT1A8CLE/testproject.rar.html


回答1:


If you block the main thread for too long, you will get an ANR. There's no way around it. Don't try to make your app wait. Just initiate your "other things" at the end of the LongProcess in the separate thread.

What you can try is use a Timer to operate it, but that will just be a very stupid thing to do, Android is overall slow, but this is your mistake. There really isnt a way around it with the way you are trying. You can try and declare an OnTerminate event for the thread to notify the main thread that the work is completed or find a different way without Sleep()



来源:https://stackoverflow.com/questions/25661569/application-isnt-responding-delphi-xe6-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!