全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:953
推到 Plurk!
推到 Facebook!

Thread的觀念問題

答題得分者是:lu
李國維
高階會員


發表:42
回覆:287
積分:235
註冊:2003-02-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-11 18:06:53 IP:219.84.xxx.xxx 未訂閱
想請問大大們: 如果我建立了一各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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-11 18:35:43 IP:221.169.xxx.xxx 未訂閱
引言: 想請問大大們: 如果我建立了一各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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-12 14:28:24 IP:219.84.xxx.xxx 未訂閱
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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-11-12 15:32:50 IP:211.76.xxx.xxx 未訂閱
李國維你好: Init中用到了VCL的元件,因此有必要以Synchronize來呼叫。不過Synchronize不接受有參數的函式,因此需要有一另一個地方拿來存放這些參數。
artist1002
高階會員


發表:2
回覆:155
積分:151
註冊:2002-09-26

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-11-12 16:18:08 IP:210.58.xxx.xxx 未訂閱
引言: 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 想請大大替我解答
嗯...你的TestID好像沒宣告
李國維
高階會員


發表:42
回覆:287
積分:235
註冊:2003-02-07

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-11-12 16:52:13 IP:219.84.xxx.xxx 未訂閱
感謝各位大大的幫忙: 小第找到問題.就是小弟宣各TestID宣各錯誤ㄌ. 我宣告成全域變數ㄌ我把TestID放到protected:那可以ㄌ.
系統時間:2024-04-28 9:44:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!