简体   繁体   English

线程设置后,Asynctask没有超时,在一段时间后取消它

[英]Asynctask is not getting timeout after a thread setup to cancel it after a certain period of time

I am trying to cancel a log run AsyncTask if a certain period of time exceeds (if AsyncTask is not automatically finised) 我尝试取消日志运行AsyncTask,如果超过一定时间(如果AsyncTask没有自动完成)

Below is the code where I setup my task to start with timeout 以下是我将任务设置为从超时开始的代码

final ProfileDesc pdsc = new ProfileDesc();
    pdsc.execute();
    Thread th_pdsc = new Thread(){
        public void run()
        {
            try{
                pdsc.get(120000, TimeUnit.MILLISECONDS);
            }
            catch(Exception e){
                pdsc.cancel(true);
                ((Activity)context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                    Toast.makeText(context, "Download Time out. Please Try again later.", Toast.LENGTH_LONG).show();

                    }
                });
            }
        }
    };
    th_pdsc.start();

below is the code for my AsynTask 以下是我的AsynTask的代码

private class ProfileDesc extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
        dialogue = new ProgressDialog(MainActivity.this);
        dialogue.setTitle("Processing");
        dialogue.setMessage("Getting Header Information");
        dialogue.setIndeterminate(true);
        dialogue.setCancelable(false);
        dialogue.show();
    }
protected void onPostExecute(Void params) {
        super.onPostExecute(params);
            dialogue.dismiss();
}
protected Void doInBackground(Void... arg0) {
//long run work
return null;
}
}

After two minutes it's still running. 两分钟后,它仍在运行。 How can I set up the time out? 如何设置超时时间? Note: I have followed this link of Stack Overflow for this code. 注意:我已经遵​​循了此代码的Stack Overflow 链接

What you had 你有什么

 ProfileDesc pdsc = new ProfileDesc();
 pdsc.execute();

was enough. 够了。 There is no need for a Thread. 不需要线程。 doInBackground is invoked on the background thread. 在后台线程上调用doInBackground So you can do you Network operations in doInbackgorund . 因此,您可以在doInbackgorund网络操作。

Secondly calling get makes AsyncTask no more Asynchronous as it blocks the ui thread 其次,调用get使AsyncTask不再异步,因为它阻止了ui线程

get(long timeout, TimeUnit unit) Waits if necessary for at most the given time for the computation to complete, and then retrieves its result get(long timeout,TimeUnit unit)必要时最多等待给定时间以完成计算,然后检索其结果

I guess you have misunderstood the use of Asynctask. 我想您误解了Asynctask的使用。 Read the docs 阅读文档

http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/android/os/AsyncTask.html

You may want to check this 您可能要检查一下

Stop AsyncTask doInBackground method 停止AsyncTask doInBackground方法

Here new thread is not needed.Just remove "dialogue.setIndeterminate(true)". 这里不需要新线程。只需删除“ dialogue.setIndeterminate(true)”即可。 It means the "loading amount" is not measured.I think it's creating the problem. 这意味着“装载量”没有测量。我认为这是造成问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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