ASynchTask睡在doinbackground锁定在UI线程 [英] ASynchTask sleep in doinbackground locking the UI thread

查看:360
本文介绍了ASynchTask睡在doinbackground锁定在UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:解决。对不起球员,它发生,我认为我的同时,我的数据装载在后台线程正在发生,该分析数据的回调没有。该数据的解析花了很长的时间,那是什么锁我的线程。

编辑:它已经到了我的注意,我的首要问题(在ProgressDialog不再旋转)可以通过一个问题,我使用ProgressDialog而不是UI线程被阻塞造成的。如果是这样的话,我该如何解决呢?

编辑:澄清,这并不锁定整个节目了永远。一旦一切都被加载,进度对话框被驳回,而新的活动长大。我的问题是,虽然它被加载,整个UI锁(即,progressdialog停止纺丝)

TL; DR:Thread.sleep()方法在doInBackground()被锁定在UI线程

我有一个应用程序是,特定活动打开时,开始加载大量的数据在从我的后端的背景下,例如,大量的相关的一个日程数据。该信息将不被马上使用,但如果用户试图访问它(即通过点击时间表按钮,启动调度活性​​)可被使用。

如果用户点击计划按钮,所有的数据负载之前等待了一下,计划活动打开,显示一切都很好。我的问题是,如果他们在数据加载之前单击该按钮。

在这里我的解决方案是创建一个AsyncTask的,显示了ProgressDialog,同时定期检查,如果数据加载完成,否则睡觉。它知道数据是通过一些应用程序范围布尔变量装载完毕。我的问题是,即使Thread.sleep()方法被doinbackground(),它仍然是锁定的UI线程。内运行

我使用的是定制的AsyncTask,定义如下:

 公共类LoadWithProgressDialog扩展的AsyncTask<太虚,太虚,布尔> {
    私人ProgressDialog PD; //进度对话框
    私人字符串称号; //进度对话框的标题
    私人字符串信息; //进度对话框的身体
    私人Runnable任务; //包含code,我们要在后台运行
    私人的Runnable postTask; //执行任务结束时
    私人语境℃;
    公共LoadWithProgressDialog(上下文的背景下,串T,串M,Runnable的R,Runnable的postR){
        超();
        C =背景;
        任务= R;
        postTask = postR;
        标题= T;
        消息=米;
    }    @覆盖
    在preExecute保护无效(){
        PD = ProgressDialog.show(C,标题,消息,假的,假的);
    }    @覆盖
    保护布尔doInBackground(虚空...... PARAMS){
        task.run();
        返回true;
    }
    @覆盖
    保护无效onPostExecute(布尔结果){
        pd.dismiss();
        如果(postTask!= NULL)
            postTask.run();
    }

,并通过调用它(例如):

 如果(loadingSchedule){            LoadWithProgressDialog lwpd =新LoadWithProgressDialog(thisClass,加载,进度表,新的Runnable(){
                公共无效的run(){
                    而(loadingSchedule){
                            尝试{
                                视频下载(1000);
                            }赶上(InterruptedException的E){
                                e.printStackTrace();
                            }                    }
                }
            },新的Runnable(){
                公共无效的run(){                    意图I =新意图(thisClass,Schedule.class);
                    i.putExtra(numberIneed指数);
                    startActivity(ⅰ);
                }
            });
            lwpd.execute();

(Ⅰ缩短访问全局变量loadingSchedule,以便更容易阅读)

如何让我这个不锁UI线程?


解决方案

  

Thread.sleep()方法在doInBackground()被锁定在UI线程


Thread.sleep()方法在doInBackground()不锁UI线程,正如它的名字所说,做任务在后台异步。

您UI线程不会被锁定。它实际上是ProgressDialog弹出的AsyncTask开始前(在preExecute()方法),并保持显示前面和AsyncTask的执行过程中的基本活动阻止用户交互(在doInBackground()方法)。和AsyncTask的结束后,终于被解雇。 (在onPostExecute()方法)


  

我有一个应用程序是,特定活动打开时,开始加载大量的数据,从我的后端

背景

这是一个常见的​​情况,这将是更有效地创建一个AsyncTask的实现,在后台加载数据,而不是创建一个AsyncTask的,显示了ProgressDialog,同时定期检查,如果数据加载完成,否则睡觉。

这里退房的API ,它包含了如何正确地使用它很好的例子。

EDIT: SOLVED. Sorry guys, it occurred to me that while my loading of my data was occurring in a background thread, the callback that parsed the data was not. The parsing of the data took a very long time, and that was what was locking my thread.

EDIT: It has come to my attention that my overarching problem (the ProgressDialog no longer spinning) may be caused by a problem with my use of a ProgressDialog rather than the UI thread being blocked. If that is the case, how do I fix that?

EDIT: to clarify, this does not lock the entire program up forever. Once everything is loaded, the progress dialog IS dismissed, and the new activity is brought up. My problem is that while it is loading, the entire UI locks up (i.e. the progressdialog stops spinning)

TL;DR: Thread.sleep() in doInBackground() is locking the UI thread

I have an app that, when a specific activity opens, begins to load a lot of data in the background from my back-end, for example, a large amount of data related to a schedule. This information will not be used right away, but may be used if the user attempts to access it (i.e. by clicking the schedule button, and launching the schedule activity).

If the user waits a bit before clicking the schedule button, all of the data loads, the schedule activity opens, and displays everything great. My problem is if they click the button before the data loads.

My solution here was to create an ASyncTask that shows a ProgressDialog while periodically checking if the data has finished loading, and sleeping otherwise. It knows that the data is finished loading via some application-wide boolean variables. My problem is that, even though the Thread.sleep() is run within doinbackground(), it is still locking the UI Thread.

I am using a custom ASyncTask, defined below:

public class LoadWithProgressDialog extends AsyncTask<Void, Void, Boolean>{
    private ProgressDialog pd; //the progress dialog
    private String title; //the title of the progress dialog
    private String  message; //the body of the progress dialog
    private Runnable task; //contains the code we want to run in the background
    private Runnable postTask; //execute when the task ends
    private Context c;


    public LoadWithProgressDialog(Context context,String t, String m,Runnable r, Runnable postR){
        super();
        c = context;
        task = r;
        postTask = postR;
        title = t;
        message = m;
    }

    @Override
    protected void onPreExecute(){
        pd = ProgressDialog.show(c,title, message, false, false);
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        task.run();
        return true;
    }


    @Override
    protected void onPostExecute(Boolean result) {
        pd.dismiss();
        if(postTask != null)
            postTask.run();
    }

and calling it via (for example):

if(loadingSchedule){

            LoadWithProgressDialog lwpd = new LoadWithProgressDialog(thisClass,"Loading","Loading Schedule", new Runnable() {
                public void run(){
                    while(loadingSchedule){
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                    }
                }
            },new Runnable() {
                public void run(){

                    Intent i= new Intent(thisClass, Schedule.class);
                    i.putExtra("numberIneed",index);
                    startActivity(i); 
                }
            });
            lwpd.execute();

(I shortened access to the global variable to "loadingSchedule" to make it easier to read)

How do I make this not lock the UI thread?

解决方案

Thread.sleep() in doInBackground() is locking the UI thread

Thread.sleep() in doInBackground() does not lock UI thread, as its name stated, task do in background asynchronously.

You UI thread doesn't get locked. It is actually the ProgressDialog pop up before AsyncTask started (in onPreExecute() method) and keep showing in front and block user interaction with the underlying activity during AsyncTask execution (in doInBackground() method). and finally dismissed after AsyncTask finish. (in onPostExecute() method)

I have an app that, when a specific activity opens, begins to load a lot of data in the background from my back-end

This is a common use case, it will be more efficient to create an AsyncTask implementation that load data in background, instead of create an ASyncTask that shows a ProgressDialog while periodically checking if the data has finished loading, and sleeping otherwise.

Check out the API here, it contains good example of how to use it properly.

这篇关于ASynchTask睡在doinbackground锁定在UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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