Thread的觀念問題 |
答題得分者是:lu
|
李國維
高階會員 發表:42 回覆:287 積分:235 註冊:2003-02-07 發送簡訊給我 |
想請問大大們:
如果我建立了一各TThread(用"File"->"New"->"ThreadObject"的方式)
且此Thread的名稱為TestThread.
我在主要的Form上面的CODE是這樣的
TestThread *MyTest[2]; void __fastcall TForm1::Button1Click(TObject *Sender) { MyTest[0] = new TestThread(true); MyTest[0]->Resume(); MyTest[1] = new TestThread(true); MyTest[1]->Resume(); }1.請問一下我這樣會執行1各Thread還是2各Thread. 2.我有沒有什麼方法可以只寫一各Thread然後去呼叫很多次,又是獨立運作的 |
lu
高階會員 發表:11 回覆:189 積分:195 註冊:2003-11-19 發送簡訊給我 |
引言: 想請問大大們: 如果我建立了一各TThread(用"File"->"New"->"ThreadObject"的方式) 且此Thread的名稱為TestThread. 我在主要的Form上面的CODE是這樣的 TestThread *MyTest[2]; void __fastcall TForm1::Button1Click(TObject *Sender) { MyTest[0] = new TestThread(true); MyTest[0]->Resume(); MyTest[1] = new TestThread(true); MyTest[1]->Resume(); int c=0; } 1.請問一下我這樣會執行1各Thread還是2各Thread. 2.我有沒有什麼方法可以只寫一各Thread然後去呼叫很多次,又是獨立運作的1.請問一下我這樣會執行1各Thread還是2各Thread. 答:2各 2.我有沒有什麼方法可以只寫一各Thread然後去呼叫很多次,又是獨立運作的 答:就是你這個寫法 你可以在上面紅字的程式碼將程式停下來,然後按CTRL + ALT + T 觀看共產生了幾個THREAD ========================= 大家一起快樂寫程式 |
李國維
高階會員 發表:42 回覆:287 積分:235 註冊:2003-02-07 發送簡訊給我 |
LU大大:
我在請教一下,我Thread的程式是這樣ㄉ
//.h class TestThread : public TThread { private: int AreaX,AreaY; protected: TLabel *LblTest; void __fastcall Execute(); void __fastcall ShowDT(); void __fastcall Init(int X,int Y); public: __fastcall TestThread(bool CreateSuspended); void __fastcall SetArea(int X,int Y,int iID); }; //.cpp int TestID; //--------------------------------------------------------------------------- // Important: Methods and properties of objects in VCL can only be // used in a method called using Synchronize, for example: // // Synchronize(UpdateCaption); // // where UpdateCaption could look like: // // void __fastcall TestThread::UpdateCaption() // { // Form1->Caption = "Updated in a thread"; // } //--------------------------------------------------------------------------- __fastcall TestThread::TestThread(bool CreateSuspended) : TThread(CreateSuspended) { } //-------------------------------------------------------------------- void __fastcall TestThread::Execute() { //---- Place thread code here ---- Init(AreaX,AreaY); while(!Terminated) { Synchronize(ShowDT); Sleep(10); } } //-------------------------------------------------------------------- void _fastcall TestThread::Init(int X,int Y) { LblTest = new TLabel(Application); LblTest->Parent = Form1; LblTest->Top = X; LblTest->Left = Y; } //-------------------------------------------------------------------- void __fastcall TestThread::SetArea(int X,int Y,int iID) { AreaX = X; AreaY = Y; TestID = iID; } //-------------------------------------------------------------------- void __fastcall TestThread::ShowDT() { LblTest->Caption = IntToStr(TestID) ":" DateTimeToStr(Now()); }問題就是說,我在主頁面按照下列的方法開啟兩各Thread MyTest[0] = new TestThread(true); MyTest[0]->SetArea(100,100,1); MyTest[0]->Resume(); MyTest[1] = new TestThread(true); MyTest[1]->SetArea(200,200,2); MyTest[1]->Resume(); 但是顯示出來的兩各label的TestID都是2 想請大大替我解答 |
pwipwi
版主 發表:68 回覆:629 積分:349 註冊:2004-04-08 發送簡訊給我 |
|
artist1002
高階會員 發表:2 回覆:155 積分:151 註冊:2002-09-26 發送簡訊給我 |
引言: LU大大: 我在請教一下,我Thread的程式是這樣ㄉ嗯...你的TestID好像沒宣告//.h class TestThread : public TThread { private: int AreaX,AreaY; protected: TLabel *LblTest; void __fastcall Execute(); void __fastcall ShowDT(); void __fastcall Init(int X,int Y); public: __fastcall TestThread(bool CreateSuspended); void __fastcall SetArea(int X,int Y,int iID); }; //.cpp int TestID; //--------------------------------------------------------------------------- // Important: Methods and properties of objects in VCL can only be // used in a method called using Synchronize, for example: // // Synchronize(UpdateCaption); // // where UpdateCaption could look like: // // void __fastcall TestThread::UpdateCaption() // { // Form1->Caption = "Updated in a thread"; // } //--------------------------------------------------------------------------- __fastcall TestThread::TestThread(bool CreateSuspended) : TThread(CreateSuspended) { } //-------------------------------------------------------------------- void __fastcall TestThread::Execute() { //---- Place thread code here ---- Init(AreaX,AreaY); while(!Terminated) { Synchronize(ShowDT); Sleep(10); } } //-------------------------------------------------------------------- void _fastcall TestThread::Init(int X,int Y) { LblTest = new TLabel(Application); LblTest->Parent = Form1; LblTest->Top = X; LblTest->Left = Y; } //-------------------------------------------------------------------- void __fastcall TestThread::SetArea(int X,int Y,int iID) { AreaX = X; AreaY = Y; TestID = iID; } //-------------------------------------------------------------------- void __fastcall TestThread::ShowDT() { LblTest->Caption = IntToStr(TestID) ":" DateTimeToStr(Now()); }問題就是說,我在主頁面按照下列的方法開啟兩各Thread MyTest[0] = new TestThread(true); MyTest[0]->SetArea(100,100,1); MyTest[0]->Resume(); MyTest[1] = new TestThread(true); MyTest[1]->SetArea(200,200,2); MyTest[1]->Resume(); 但是顯示出來的兩各label的TestID都是2 想請大大替我解答 |
李國維
高階會員 發表:42 回覆:287 積分:235 註冊:2003-02-07 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |