PDA

View Full Version : مشکل در استاپ کردن thread



CodeforLife
دوشنبه 22 شهریور 1395, 23:17 عصر
با سلام من برنامه ای نوشتم که وقتی روی یه دکمه ای کلیک میشه ؛ در یه thread یه کاری رو انجام بده و کار تمام میشود .
حالا وقتی دوباره روی دکمه کلیک میکنم ، یه exception دارم که میگه threas is start .
یعنی استاپ نشده!!!!
من هرجای برنامه که می تونستم استاپ کردم ولی نشد .
کدهام رو میگذارم .لطف کنید کمکم کنید .



public class CounterThread extends Thread {
public CounterThread() {
}

private int secondCount;

public int getSecondCount() {
return secondCount;
}

public void setSecondCount(int secondCount) {
this.secondCount = secondCount;
}

private TextView txtResult;

public TextView getTxtResult() {
return txtResult;
}

public void setTxtResult(TextView txtResult) {
this.txtResult = txtResult;
}

private Button btnStart;

public Button getBtnStart() {
return btnStart;
}

public void setBtnStart(Button btnStart) {
this.btnStart = btnStart;
}

@Override
public void run() {
super.run();
//
while (secondCount > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

secondCount--;
G.HANDLER.post(new Runnable() {
@Override
public void run() {
int min = (int) Math.floor(secondCount / 60);
int sec = secondCount % 60;

if (sec < 10) {
txtResult.setText("0" + String.valueOf(min) + " : " + "0" + String.valueOf(sec));
} else {
if (min == 0) {
txtResult.setText("0" + String.valueOf(min) + " : " + String.valueOf(sec));
} else if (min != 0 && min < 10) {
txtResult.setText("0" + String.valueOf(min) + " : " + String.valueOf(sec));
} else if (min != 0 && min > 10) {
txtResult.setText(String.valueOf(min) + " : " + String.valueOf(sec));
}
return;
}
if (secondCount == 0) {
btnStart.setEnabled(true);

}
}

});
}
}








EditText edtSecond;
TextView txtResult;
Button btnStart;
CounterThread counterThread = new CounterThread();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//================================================== ====================================

edtSecond = (EditText) findViewById(R.id.edtSecond);
txtResult = (TextView) findViewById(R.id.txtResult);
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startWork();
}
}
);
}

private void startWork() {
String secStr = edtSecond.getText().toString();
edtSecond.setText("");
if (secStr.length() > 0 && Integer.parseInt(secStr) <= 1200) {
counterThread.setSecondCount(Integer.parseInt(secS tr));
counterThread.setBtnStart(btnStart);
counterThread.setTxtResult(txtResult);
counterThread.start();
btnStart.setEnabled(false);

return;
} else if (secStr.length() == 0 || Integer.parseInt(secStr) > 1200) {
Toast.makeText(Activity.this, "sdsdsd", Toast.LENGTH_SHORT).show();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_, menu);
menu.add(0, 1, 1, "راهنما");
return true;
}

@Override
protected void onDestroy() {
super.onDestroy();
counterThread.stop();
}

@Override
protected void onPause() {
super.onPause();
counterThread.stop();
}

shahin bahari
سه شنبه 23 شهریور 1395, 00:44 صبح
هر ترد فقط 1 بار میتونه استارت بشه. مهم نیست که کارش تموم شده یا نه. هربار که دکمه فشار داده شد یک آبجکت از ترد بسازید و اون رو استارت کنید....

CodeforLife
سه شنبه 23 شهریور 1395, 07:57 صبح
سلام وممنونم
من هم همین کار رو کردم .
و یه آبجکت ساختم که هر بار فشار داده شد استارت بشه !
ولی بار دوم برنامه کرش میکنه به دلیل اینکه می گه ترد هنوز استارت هستش !!!!!
من کاملا گیج شدم آیا باید stop کنمش؟ کجا ؟
خواهش میکنم راهنماییم کنید

shahin bahari
چهارشنبه 24 شهریور 1395, 01:58 صبح
سلام وممنونم
من هم همین کار رو کردم .
و یه آبجکت ساختم که هر بار فشار داده شد استارت بشه !
ولی بار دوم برنامه کرش میکنه به دلیل اینکه می گه ترد هنوز استارت هستش !!!!!
من کاملا گیج شدم آیا باید stop کنمش؟ کجا ؟
خواهش میکنم راهنماییم کنید

شما در کل فقط یک بار new CounterThread(); داخل برنامه صدا میزنید. یعنی یک ابجکت دارید که هر بار سعی می کنید اون رو استارت کنید و با توجه به اینکه هر ترد فقط یک بار میتونه استارت بشه،خطا میده. این داستان ربطی به استاپ بودن نداره!
شما برای رویداد فشار دکمه یه متغیر جدید تعریف کن و اون رو استارت کن.....

CodeforLife
پنج شنبه 25 شهریور 1395, 10:25 صبح
با تشکر
منظور شما این هست که من از setOnclicklistener یه آبجکت بگیرم ؟؟؟؟

spiderman200700
پنج شنبه 25 شهریور 1395, 12:46 عصر
نخیر.
منظوزشون اینه که این خط رو:
CounterThread counterThread = new CounterThread();
ببرید توی متد startWork