应用程序无响应-Delphi XE6-Android [英] Application isn't responding - Delphi XE6 - Android

查看:149
本文介绍了应用程序无响应-Delphi XE6-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android上,当我在单击后15秒内执行长功能(所需时间> 30秒)时触摸屏幕时,出现消息应用程序无响应". 我不想看到它.

我做了一个测试项目,带有2个按钮和一个功能"LongProcess",用于模拟一个较长的过程(这只是30秒钟的睡眠时间).我的第一个按钮"LaunchFunction"仅调用该函数.我的第二个按钮"LaunchThread",启动一个线程,该线程将执行我的"LongProcess". 在第一种情况下,我遇到了问题,但在第二种情况下,它运行正常(该消息永远不会出现,因为我的主要表单不在等待中.)

但是,我必须等待"LongProcess"的结尾(因此线程结束),因为我必须在它之后做其他需要"LongProcess"结果的事情.因此,我尝试使用多种方法来等待我的线程.我尝试使用TThread类的"WaitFor",但它重复了最初的问题.我还尝试了一个简单的"while".

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

但是还是一样,如果我触摸屏幕,弹出窗口将再次出现. 关键部分而不是"while"或"Thread.WaitFor"执行的操作完全相同.

因此,我试图在我的"while"中更新GUI,以向Android展示该应用程序正在运行.

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

如果我触摸屏幕,则看不到标签值的变化. 15秒后,我将弹出窗口(但我仍会在后台看到我的标签被更新).

有人有主意吗?也许我们可以在很长的过程中禁用事件(因此单击不会出现在队列中,因此15秒后不应将其视为无响应").在Android上,Application.ProcessMessage似乎不起作用. 或者,也许Android API中存在一些可以向操作系统说我们不活跃的信息?

最后,如果我单击等待",则该应用程序将正常运行.如果我也没有触摸屏幕(也没有线程,直到我不触摸屏幕),但是我看到很多用户像机器人一样单击确定"(这将关闭应用程序的...) .请为您的未来提供帮助

ps:我试图用计时器替换线程,因为我在论坛上看到了它,但没什么改变.

ps2:此处是.zip或演示项目 http://www .partage-facile.com/YOJT1A8CLE/testproject.rar.html

解决方案

如果阻塞主线程的时间过长,则会收到ANR.没有办法解决.不要试图让您的应用程序等待.只需在LongProcess末尾的单独线程中启动您的其他内容"即可.

您可以尝试使用计时器操作它,但这只是一件很愚蠢的事情,Android总体上运行缓慢,但这是您的错误.您尝试的方法确实没有解决的办法.您可以尝试为线程声明一个OnTerminate事件,以通知主线程工作已完成,或者在没有Sleep()的情况下找到其他方式

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

解决方案

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()

这篇关于应用程序无响应-Delphi XE6-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆