java - How to update something in main class in thread -
hi trying make multiplayer game find opponent in thread, not sure why code running in thread not update model in main class...
here code in main class. call loadtask starts thread
// start model, passing number of words, user name, , selected animal model = new multiplayermodel(num_words, username, anmid); model.addobserver(this); new loadtask().execute(); setcontentview(r.layout.activity_multi_player); initialdisplay(animal, background, oppanimal);
here code thread class
private class loadtask extends asynctask<void, integer, void> { // called before running code in separate thread private boolean quitflag; @override protected void onpreexecute() { progressdialog = progressdialog.show(multiplayer.this,"finding game...", "searching opponent, please wait...", false, false); } @override protected void doinbackground(void... params) { synchronized (this) { try { model.beginmatchmaking(); model.setwordslist(); // opponent's animal model oppanimal = reversedrawable(model.getopponentanimal()); // display multiplayer screen } catch (internetconnectionexception e) { e.fillinstacktrace(); quitflag = true; error(states.error.connection); return null; } catch (emptyqueueexception e) { e.fillinstacktrace(); quitflag = true; error(states.error.noopponent); return null; } catch (internalerrorexception e) { e.fillinstacktrace(); quitflag = true; error(states.error.internal); return null; } } return null; } @override protected void onpostexecute(void result) { if (!quitflag) { progressdialog.dismiss(); gametimer = new gametimer(start_time, interval); gametimer.start(); } } }
and segfaults on initialdisplay after thread called because fields in model class not updated @ all. acts though created , nothing done it
i'm not sure asynctask class if it's configurated thread , next line
new loadtask().execute();
will start thread properly, in case thread execute inparallel usualy after lines:
setcontentview(r.layout.activity_multi_player); initialdisplay(animal, background, oppanimal);
to prove may print messages main methods. solve may add next line after starting thread:
new loadtask().execute(); thread.sleep(100)
Comments
Post a Comment