Giacomo De Bacco
Giacomo De Bacco

Reputation: 723

Java Thread ,Freeze my UI

i'm trying to check the value of 2 strings every minute, and close the Thread when the value will be different , but this code freeze my UI. that's the code.

@Override
protected void onPostExecute(String result) 
{
    //Download sourcecode on String
    String secondaChar = result;
    //Strings declaration
    String First = "";
    String Second = "";
    try 
    {   
        //Writing sourcecode on string
        Write(secondaChar);
        String ReadingFile="FileSorgente";
        First=reading(ReadingFile);
        while (true) 
        {   
            // Downloading new sourcecode on string
            secondaChar = result.replaceAll("[^a-zA-Z]+", "");
            //writing new SC on string
            Scrittura(secondaChar);
            // new SC on second string
            Second = Reading(ReadingFile);
            // check until 2 strings are the same
            if(First.equalsIgnoreCase(Second))
            {
                Toast.makeText(getApplicationContext(), "Keep Checking", Toast.LENGTH_LONG).show();
            }
            else
            {         
                Toast.makeText(getApplicationContext(), "FALSE", Toast.LENGTH_LONG).show(); 
                break;
            } 
            Thread.sleep(60 * 1000);
        }
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    textView.setText("Usless");
}

Reading and Writing are 2 different method, i've test this in java befor and it worked, but probably in Android sdk i have to do something different.

Upvotes: 1

Views: 209

Answers (2)

TeeTracker
TeeTracker

Reputation: 7350

@Override
                protected void doInBackground(params....) 
                {
                    //Download sourcecode on String
                    String secondaChar = result;
                    //Strings declaration
                    String First = "";
                    String Second = "";
                    try 
                    {   
                        //Writing sourcecode on string
                        Write(secondaChar);
                        String ReadingFile="FileSorgente";
                        First=reading(ReadingFile);
                        while (true) 
                        {   
                            // Downloading new sourcecode on string
                            secondaChar = result.replaceAll("[^a-zA-Z]+", "");
                            //writing new SC on string
                            Scrittura(secondaChar);
                            // new SC on second string
                            Second = Reading(ReadingFile);
                            // check until 2 strings are the same
                            if(First.equalsIgnoreCase(Second))
                            {
                                  publishProgress( "Keep Checking" );
                            }
                            else
                            {         
                                  publishProgress( "FALSE" ); 
                                break;
                            } 
                            Thread.sleep(60 * 1000);
                        }
                    } 
                    catch (IOException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                     publishProgress("Usless");
                }

@Override
protected void onProgressUpdate(String... progress) {
         showToast(progress[0]);
}

Upvotes: 0

kamituel
kamituel

Reputation: 35950

You should execute long running tasks - for instance, Thread.sleep(...) - in the doInBackground() - which is running off the UI thread.

onPostExecute() is supposed to be only called after the doInBackground() completes, and is used exactly to update the UI - thus it runs on the UI thread. Every delay you make there will cause your app to become unresponsive.

To fix this, move your logic to doInBackground(). Remeber that all the UI operations, though, have to happen on the UI thread. So, for instance, showing toasts would have to be done from onPostExecute() or from onProgressUpdate().

See this excellent answer for an example on how to do this. Note that Thread.sleep() is called there as well, but in doInBackground().

Upvotes: 1

Related Questions