Ankuj
Ankuj

Reputation: 723

UI thread freezes

I am creating a new thread using the following function.

public void toCallAsynchronous() {
    mThread = new Thread() {
        public void run() {
            while (true) {
                try {
                    // do something here
                    if(mLoggedIn)
                    {
                        boolean syncresult = mDownload.syncData();
                    }
                    Log.d(TAG, "local Thread sleeping");
                    Thread.sleep(10000);
                    //mHandler.postDelayed(this, 1000);
                } catch (InterruptedException e) {
                    Log.e(TAG, "local Thread error", e);
                }
            }
        }
    };
    mThread.start();
}

I am calling this in onResume as I want this thread to start only when user has login into his account. The synData function accesses the server for new files, downloads them and update entry into a database. Could you please identify what I am doing wrong here ?

Upvotes: 0

Views: 103

Answers (1)

Alexander
Alexander

Reputation: 48272

while(true) is strange because you, probably, don't want this thread to run forever. At the very least it should be while(!isFinishing())

Also you will want make sure mDownload.syncData doesn't try to access any UI, if it does there will be an exception.

Upvotes: 2

Related Questions