Re: [問題] 有關於Picturebox以及執行緒問題

看板C_and_CPP (C/C++)作者 (阿維)時間12年前 (2013/09/11 10:46), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串2/2 (看更多)
謝謝各位回覆 問題應該簡單說明 我現在寫一個簡單的方法 一個無限迴圈 void Showprocess() { int aaaa = 0 ; while(true) { maskedTextBox2->Text = aaaa.ToString() ; aaaa++; } } 並在另外一個按鍵事件裡面啟動執行緒去執行這個方法 ThreadStart^ threadDelegate2 = gcnew ThreadStart( this, &Form1::Showprocess ); Thread^ newThread2 = gcnew Thread( threadDelegate2 ); newThread2->Start(); 他就會出現這樣錯誤 Additional information: Cross-thread operation not valid: Control 'maskedTextBox2' accessed from a thread other than the thread it was created on. 好像原件無法在執行緒裡面被執行 如果我非得執行 請問到底該怎麼做呢? ※ 引述《godzilla1954 (阿維)》之銘言: : 我有一個程式 他是要將影片讀進來 然後我要使用Picturebox畫出來 : 有點長 我盡量寫詳細一點 : 所以我寫的簡單的class 大概是這樣 : ref class StreamReadShow : { : public: : System::Drawing::Bitmap^ _bmpImg ; : System::Windows::Forms::PictureBox^ pictureBox; : StreamReadShow( System::Windows::Forms::PictureBox^ pB ) : { : pictureBox = pB ;//指定哪一個picturebox : } : void StreamReadShowProcess() : { : while(true) : { : if(cvGrabFrame(capture )) : { : //讀影片 將影片資料取出儲存到_bmpImg : //讀影片那段就略過.....反正最後可以得到_bmpImg : //BMP放到指定的picturebox : pictureBox->Image = _bmpImg ; : pictureBox->Refresh(); : } : } : } : 我省略一些東西 這個class我說明一下 : 大概就是我會從外面傳進指定的PictureBox物件 : 然後把影片結取出來的bmp畫上指定的PictureBox : 所以程式大概就會出現播放影片的效果 : 而呼叫這個class方法我是寫在Form介面的按鍵事件 : 所以理所當然按下按鍵事件 影片會播放 : 但是程式也會因為讀影片的迴圈給鎖住 : 我這邊使用執行緒 解決這問題 : //起始物件 並指定要畫在哪一個picturebox : StreamReadShow^ SRS = gcnew StreamReadShow( pictureBox1) ; : //執行緒 : ThreadStart^ threadDelegate = gcnew ThreadStart( SRS, &StreamReadShow::StreamReadShowProcess ); : Thread^ newThread = gcnew Thread( threadDelegate ); : newThread->Start(); : 使用執行緒去執行StreamReadShowProcess這個影片迴圈 : 問題來了 : 我發現執行到 : pictureBox->Image = _bmpImg ; : pictureBox->Refresh(); : 就會錯誤 : Additional information: Object is currently in use elsewhere. : Additional information: Cross-thread operation not valid: Control 'pictureBox1' : accessed from a thread other than the thread it was created on : 頭很大 我研究一下午 好像事執行緒問題 : 拿掉事沒問題 : 請問各位先進 有沒有辦法去解決這樣的問題 真的搞好久 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.164.252.199

09/11 13:22, , 1F
我現在改用maskedTextBox2->BeginInvoke()去包
09/11 13:22, 1F

09/11 13:25, , 2F
是可以 但整個Form會被LOCK住 有辦法不要被LOCK住嗎?
09/11 13:25, 2F
文章代碼(AID): #1IBzbnyZ (C_and_CPP)
文章代碼(AID): #1IBzbnyZ (C_and_CPP)