推 Blueshiva: 你應該是開個thread去跑迴圈,不是在迴圈中跑thread 03/07 15:39
→ yuanruo: 因為你叫main thread 睡覺了,所以把UI線程卡住了 03/07 21:18
謝謝解惑 現在改用
t1 = [[NSThread alloc]initWithTarget:self selector:@selector(Method) object:nil];
[t1 start];
寫在一個Button下面,然後想用另一個Button去停止Thread卻停止不下來
我另一個Button下是寫
[t1 cancel];
但是按下去後沒有任何反應,迴圈持續執行中
請問該怎麼做才能達成停止該selector的效果呢?謝謝!
※ 編輯: aureolus (60.248.134.2), 03/08/2016 11:42:06
→ yuanruo: NSThread的cancel只是設一個flag 並不會馬上取消已執行的 03/08 12:24
→ yuanruo: 你需要在main thread中看thread的isCancel屬性 用exit 03/08 12:25
→ yuanruo: 不然嫌麻煩直接執行exit 也行 03/08 12:29
推 Blueshiva: 應該不是在main thread,而是t1中看並exit吧 @.@ 03/08 12:57
誠心感謝各位的幫忙,已經順利解決了問題
就誠如各位所說再用些判斷式去exit目前執行中的thread
換個方向想後就成功解決了問題,謝謝幫忙!
※ 編輯: aureolus (60.248.134.2), 03/08/2016 14:29:09
→ yuanruo: 如果沒用property存thread 那在t1中取得currentThread 03/08 15:23
→ yuanruo: 再exit, 有存起來我覺得哪個thread都能exit 吧(?) 03/08 15:24
→ yuanruo: 打錯 不是在t1中是執行thread的method中當前thread exit 03/08 15:25
推 Blueshiva: 問題是你在main thread中exit會把main thread結束掉 XD 03/08 15:42
→ Blueshiva: 反正要搞清楚自己要把哪個thread停掉就是了啦~ 03/08 15:43
我是設定一個變數去判斷有沒有按下停止Button
如果有直接break檢測的迴圈 所以實際說起來並沒有停止Thread
功能上是實現了 但是實際上Thread還在跑 只是沒有判斷我要的東西而已
結果而言是成功的...我在找時間去研究這個區塊好了 謝謝兩位的幫忙!
※ 編輯: aureolus (60.248.134.2), 03/08/2016 16:46:46
→ yuanruo: 喔 我那樣打是他有用t1存起來 當然是在main看t1.isCancel 03/08 17:09
推 Blueshiva: +exit: Terminates the current thread. 所以應該還是 03/09 11:33
→ Blueshiva: 要在t1中呼叫才對,而且exit是個class method,不能針 03/09 11:33
→ Blueshiva: 對某個instance呼叫 03/09 11:34