Re: [問題] 從 main thread perform 其他的 thread
※ 引述《zonble (zonble)》之銘言:
: ※ 引述《sinread (電腦真耗錢)》之銘言:
: : 小弟有一段code如下, 請各位先進給我一點意見:
: : - (void)StreamStart
: : {
: : NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
: : while(!exitNow)
: : {
: : [videoStream Connect];
: : }
: : [pool release];
: : }
: : 當UIPickerView被選取到某個row, 會變換 channel, 但是我目前這樣的寫法, 無法從
: : main thread 去 perform streamThread,
: : 請問我該怎麼做才可以讓兩個thread 溝通?
: 我們來讀一下文件,performSelector:onThread:withObject:waitUntilDone:
: 的解釋這麼說:
: This method queues the message on the run loop of the target
: thread using the default run loop modes
: 在你的 selector 裡頭,並沒有開 runloop,所以程式不斷在那段迴圈
: 裡頭跑,外面的 message 想來也因此傳不進去。
所以我應該在這個裡面開一個runloop, 如果我理解沒錯的話,
1. 加上main thread的runloop 就有兩個囉?
2. 還是說要將這stram的thread加入main thread的runloop (@_@ 繞口令XD)?
如果是"1" 我在網路上找到一個範例:
- (void)StreamStart
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
while(!exitNow)
{
[videoStream Connect];
}
[pool release];
}
如果是"2"的話應該要怎麼做??
: 我試著了解你的程式的意圖,大抵上就是在背景用迴圈不斷的用 connect
: 這個 method 來取得 video stream 的資料,而值此同時又想要可以改變
: video stream 的 channel id。想想好像不一定要把改 channel 這件事情
: 丟到另外一個 thread 來做,大不了可以把現在的 Thread 取消掉,改了
: channel id 之後再另外開一個 Thread,我也不清楚每次 connect 會多久,
: 如果每次 connect 的時間都很短的話,也可以把 channel id 寫到別的地
: 方去,每次迴圈裡頭的東西開始前,都先問問使用者要的 channel id 與
: videoStream 物件裡頭的 channel id 是不是一樣。
: 話說這是在寫像 AverTV 那樣的東西嗎?(歪頭)
如果是把thread關掉, 不是也需要設定exitNow這個變數(非全域變數)~
但是我修改他好像也沒反應XD!! 不過最重要的理由:是我想學習
怎麼樣在兩個thread間傳遞參數。
這個程式就是個video straming, 有收jpeg跟h.264兩種data,
我現在只進行到jpeg的階段, 由於server端的關係,
規定就是每次連線只能要一張jpeg, 所以只好一直連一直連, haha ! XD讓您見笑了^^
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.24.63.50
討論串 (同標題文章)
MacDev 近期熱門文章
PTT數位生活區 即時熱門文章