RunonUI Thread blocks a AsynTask in android from executing to completion -


i have issue runonuithread , asynctask getting called together.

my asynchtask gets data populate listview through runonuithread call. asych task can data when ui not in focus . starts ui screen , runs until application logged out. data coming task can populate particular listview. if invoke asynch task view using call executeonexecutor call asynchtask, asynch task not run compeltion. locks up. if comment out code never ending asychtask called receiver.. ui's listview populated , no asych task locks. receiver waits on rest api call response return since running through executeonexecutor call, should parallel processing. need have receiver running time integral of application. strategy can use here fix issue. here code snippets.

public class receiver  {     private final static string queue_name = "hello";      private string m_errormessage;     private irunonuithreadcallback irunonuithreadcallback;    private send m_received;     private int m_timeoutduration;//how long reading of new message waits in milli seconds      public void setcallback(irunonuithreadcallback runonuithreadcallback)     {         irunonuithreadcallback = runonuithreadcallback;     }     public void settimeoutduration(int timeout)     {         m_timeoutduration = timeout;     }     public void startreceiver(send receiverinfo)     {          string receivedinfo = null;         try {             new receiveinfo ().executeonexecutor(asynctask.thread_pool_executor, receiverinfo);         }         catch(exception exp)         {             m_errormessage = exp.getmessage();         }     }     private class receiveinfo extends asynctask<send, void, send>     {         //initiate vars         public receive() {             super();             //my params here         }         protected message doinbackground(send... receiverinfo)         {                             send recv=null;             try {                  preferencesingleton single = preferencesingleton.getinstance();                 final user user = single.getuser();                 final svcapi svc = loginauthsvc.init();                   send send=(send)receiverinfo[0];                 send.setuserid(user.getusername());                 //dxbrem                 while (true) {                      recv=svc.receive(send);                     string str= recv.get();                     if ((str == null || (str.trim().length() == 0))) {                         continue;                     }                      //dj uncomment                    irunonuithreadcallback.runafterisreceived(recv);                     //messages.add(message);                     system.out.println(" [x] received '" + recv + "'");                  }              }catch(exception exp)             {                 m_errormessage = exp.getmessage();             }              return recv;         }     }     public string geterrormessage() {         return m_errormessage;     } }  public interface irunonuithreadcallback {     public void runafterisreceived(bytesent m);     public void runafterisreceived(send m); } 

the class handles this.. has following code ,

 public class mainfragment extends fragment implements mfragment.onfragmentinteractionlistener, irunonuithreadcallback {      private receiver mreceiver;      public void setuicallbackonmessagereceiver()         {             mreceiver.setcallback(this);         }       private void callrunuithread(final sentinfo m) {             getactivity().runonuithread(new runnable() {                 public void run() {                     if (m!= null) {                         mgridarray.add(message);                         if (mlistadapter != null) {                             mlistadapter.notifydatasetchanged();                             mlistview.setselection(mlistadapter.getcount());                             mlistview.smoothscrolltoposition(mlistadapter.getcount());                         }                     }                 }             }); // end of runonuithread         }       @override         public void runafterisreceived(bytesent m) {          }          @override             public void runafterisreceived(sent m) {                 sentinfo m= new sentinfo(false, recv.getinfo());                 callrunuithread(msg);             } 

mlistadapter listadapater mlistview listview

here asynchtask code

import android.app.activity; import android.app.fragment;  import android.content.context; import android.os.asynctask; import android.util.log;    import java.util.concurrent.callable; import java.util.concurrent.executor;  public class callabletask<t> extends asynctask<void,double,t> {      private static final string tag = callabletask.class.getname();          public static <v> void invoke(callable<v> call,activity activity, taskcallback<v> callback){          new callabletask<v>(activity,call, callback).executeonexecutor(asynctask.thread_pool_executor );     }         private callable<t> callable_;     private asynctask<void, void, string> asynctask_;     private context context;     private activity activity;     private fragment fragmentactivity;     private android.support.v4.app.fragment dynamicfragment;      private taskcallback<t> callback_;      private exception error_;      public callabletask(fragment actvy,callable<t> callable, taskcallback<t> callback) {         callable_ = callable;         callback_ = callback;         fragmentactivity=actvy;     }      public callabletask(activity actvy,callable<t> callable, taskcallback<t> callback) {         callable_ = callable;         callback_ = callback;         activity=actvy;     }      @override     protected t doinbackground(void... ts) {         t result = null;         try{             result = callable_.call();         } catch (exception e){             log.e(tag, "error invoking callable in asynctask callable: " + callable_, e);             error_ = e;         }         return result;     }      @override     protected void onpostexecute(t r) {         if(error_ != null){             callback_.error(error_);         }         else {             callback_.success(r,activity);         }     }       public static <v> void invoke(callable<v> call, fragment _frg, taskcallback<v> callback) {         new callabletask<v>(_frg,call, callback).executeonexecutor(asynctask.thread_pool_executor );     }   //    public callabletask(android.support.v4.app.fragment chatactivity,callable<t> callable, taskcallback<t> callback) { //        callable_ = callable; //        callback_ = callback; //        dynamicfragment=chatactivity; //    } public callabletask(android.support.v4.app.fragment actvy,callable<t> callable, taskcallback<t> callback) {     callable_ = callable;     callback_ = callback;     dynamicfragment=actvy; }      public static <v> void invoke(callable<v> call, android.support.v4.app.fragment _frg, taskcallback<v> callback) {         new callabletask<v>(_frg,call, callback).executeonexecutor(asynctask.thread_pool_executor );     } } 

this gets called here... when clicking on button send.

 callabletask.invoke(new callable<sent>() {               @override             public sent call() throws exception {         }, this, new taskcallback<sent>() {              @override             public void success(sent result, context context) { mgridarray.add(result);                             if (mlistadapter != null) {                                 mlistadapter.notifydatasetchanged();                                 mlistview.setselection(mlistadapter.getcount());                                 mlistview.smoothscrolltoposition(mlistadapter.getcount()); } @override             public void error(exception e) {            }         }); 

thanks dhiren

i resolved running asynch.cancel call on thread activity fragment started thread. when move away activity. if did not , blocked other tasks running,


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -