Note that there are some explanatory texts on larger screens.

plurals
  1. POProgress dialog while running computation task in background (AsyncTask)
    text
    copied!<p>I am very confused, i am trying for two days to make animation on an image while my engine is thinking of a move (this is a game app). I execute the chooseMove() method inside AsyncTask object because it is a little heavy recursive function that may take some time,and also i want to rotate an hourglass image while the engine is thinking. and i tried to do it every way i know: another AsyncTask, Android's own animation class,handlers etc'. but no matter what i do it seems that i can't make it work. if i try to execute two threads at the same time,it only executes one of them, and the same thing happens with android own animation class. so i tried to use a progress dialog just to see that i am not getting crazy,and guess what.. same problem! I show the progress dialog in the onPreExecute() ,but the doInBackgroun() never gets done! the progress dialog take control over the whole app for some reason. how should i do it? i though that the progress dialog is meant for this kind of things. thx!</p> <p>EDIT: this is the code of my async task class. as you can see, i am showing a progress dialog in the onPreExecute() ,and dismissing it in the onPostExecute. but the onPost never gets called because the doInBackground() never gets called either. </p> <pre><code>protected void onPreExecute() { super.onPreExecute(); activity.setPlaying(true); if (android.os.Build.VERSION.SDK_INT &gt;android.os.Build.VERSION_CODES.GINGERBREAD) { // only for gingerbread and newer versions activity.invalidateOptionsMenu(); } progressDialog = new ProgressDialog(activity); progressDialog.setMessage("Thinking..."); progressDialog.setIndeterminate(true); progressDialog.setCanceledOnTouchOutside(false); progressDialog.show(); } @Override protected Integer doInBackground(Void... arg0) { int engineWin = board.takeWin(board.getBoard(), engine.getNumberOfEngine()); int opponentWin = board.takeWin(board.getBoard(), engine.getNumberOfOpponent()); if(engineWin!=-1){ try{ Thread.sleep(500); } catch(Exception e){} return engineWin; } else if(opponentWin!=-1){ try{ Thread.sleep(500); } catch(Exception e){} return opponentWin; } else{ if(engine.isEnginesTurn()&amp;&amp;!Board.checkGameOver(board.getBoard())){ int[] newBoard = new int[42]; System.arraycopy(board.getBoard(), 0, newBoard, 0, 42); return engine.chooseMove(engine.isEnginesTurn(),newBoard,-500001,500001,0,4).getMove(); } else{ return -1; } } } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); progressDialog.dismiss(); if(result!=-1){ ballAnimTask = new BallAnimTask(activity,boardView,board,engine); ballAnimTask.execute(findSlotNumberByIndex(result)); } } </code></pre> <p>this is the recursive chooseMove() i am calling in doInBackground(). before i tried to show the progress dialog,everything worked just fine. there is no problem with this function or any other function for that matter. only when i tried to do animations or dialogs at the same time,i got issues. the chooseMove() is physically on another class and i am only calling it from the AsyncTask. maybe this is the problem?? </p> <pre><code>public Best chooseMove(boolean side,int[]board,int alpha,int beta,int depth,int maxDepth){ Best myBest = new Best(); Best reply; int num; if(Board.checkGameOver(board)||depth==maxDepth){ myBest.setScore(returnPositionScore(board)); return myBest; } if(side){ myBest.setScore(alpha); num = numberOfEngine; } else{ myBest.setScore(beta); num = numberOfOpponent; } ArrayList&lt;Integer&gt; availableMoves = new ArrayList&lt;Integer&gt;(); availableMoves = searchAvailableMoves(board); for(int move:availableMoves){ board[move] = num; reply = chooseMove(!side,board,alpha,beta,depth+1,maxDepth); board[move] = 0; if(side&amp;&amp;reply.getScore()&gt;myBest.getScore()){ myBest.setMove(move); myBest.setScore(reply.getScore()); alpha = reply.getScore(); } else if(!side&amp;&amp;reply.getScore()&lt;myBest.getScore()){ myBest.setMove(move); myBest.setScore(reply.getScore()); beta = reply.getScore(); } if(alpha&gt;=beta){ return myBest; } } return myBest; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload