异步线程停止主 UI 线程
谁能告诉我为什么在异步线程完成之前不会显示以下对话框。我无法弄清楚这一点。这是在主 UI 线程中运行的。不知道为什么新线程会影响主 UI 线程的流程
dialog = new ProgressDialog(this);
dialog.show();
new Thread(new Runnable() {
public void run() {
while(imageLoader.isProcessing()) {}
doSomething();
}
}).run();
Can anyone tell me why the following dialog box does not show until the asynchronous thread has finished. I cannot figure this one out. This is running in the main UI thread. Not sure why a new thread would affect the flow of the main UI thread
dialog = new ProgressDialog(this);
dialog.show();
new Thread(new Runnable() {
public void run() {
while(imageLoader.isProcessing()) {}
doSomething();
}
}).run();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要调用匿名线程的
start()
方法,而不是run()
方法。来自文档:
You need to call the
start()
method of the anonymous Thread, not therun()
method.From the docs:
启动方法
调用我推荐使用 AsyncTask 的
请参阅 this ,它具有适当的线程处理机制
另请参阅此示例
call the start method
i reccomend to use use AsyncTask
see this , it has proper thread handling mechanism
see this example as well
不要期望线程遵循代码流。我建议使用 AsyncTask 并显示对话框,您可以在 onPreExecute() 中显示对话框并在 onPostExecute() 中删除它
,或者您可能想尝试 runOnUiThread()
Don't expect threads to follow the flow of your code. I suggest to use AsyncTask and for showing the dialog you can show the dialog in onPreExecute() and remove it in onPostExecute()
or may be you like to try runOnUiThread()