Search code examples
androidmultithreadingdialogthread-safetywaithandle

Progress bar to show only during long operations


I am designing an application that fetches data from a sqlite database on the click of a button . I want to display a wait message or progress bar during the time of the fetch only if the process is long like say more than 3 secs . Otherwise it can just proceed with the program . How do i do this . i tried showing a progress dialog using the following code but it only waits for the specified sleep time and proceeds without showing anything .... plss help

protected void GetOrders() 
{
    ProgressDialog dialog = null;
    try
    {
        dialog=ProgressDialog.show(loginScreen.this,"PLEASE WAIT","LOADING CONTENTS ..",true);
        //Accesses database 
        allOrders=ProductionOrdersBL.GetOrder();
        Thread.sleep(4000);

    }
    catch(Exception e){}
    finally
    {
        dialog.dismiss(); 
    }
}

Solution

  • I found the solution ... I had some operations after the call of the execute method . Thus while the doInBackground() method was executing in a non-UI thread , my UI thread simply continued executing the code that followed the execute method call ... The solution is to perform no task in the main UI thread following the execute method call :)