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

thread的問題,我一直解不出來..

尚未結案
liang-shien
一般會員


發表:15
回覆:21
積分:7
註冊:2005-05-26

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-14 22:43:06 IP:221.169.xxx.xxx 未訂閱
請問一下thread的問題,我現在有一個functionA因為要處理的較久, 所以create 一個thread去處理它,在這個functionA中它會執行到 combobox1->items->add("123") 可是當thread跑完回到主程式,我要去讀那個值時竟然讀不到ㄝ.. combobox1->items->count敬然是0....可是我debug時明明那行有跑到..重點是,假如我不要把這個functionA用thread run時,就可已讀的到combobox的值,為什麼用thread結果會這樣呢
Stallion
版主


發表:52
回覆:1600
積分:1995
註冊:2004-09-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-14 22:59:00 IP:211.22.xxx.xxx 未訂閱
請問有無使用Synchronize(Method: TThreadMethod),當Thread Method存取VCL元件時,要使用它!試試看! -----------------------------------------------
liang-shien
一般會員


發表:15
回覆:21
積分:7
註冊:2005-05-26

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-06-15 09:28:27 IP:61.220.xxx.xxx 未訂閱
我在builder5編譯時,它說Synchronize這是沒有定義的ㄝ...是要include什麼檔嗎
yhchu
一般會員


發表:2
回覆:70
積分:24
註冊:2004-01-28

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-06-15 14:22:23 IP:220.130.xxx.xxx 未訂閱
主程式有檢查該 worker thread 有執行完才去讀值嗎?因為 worker thread 跟 main thread 是同時執行的,如果 main thread 直接讀值,可能 worker thread 還沒有執行到 add("123"),那讀到 0 就很正常了。
liang-shien
一般會員


發表:15
回覆:21
積分:7
註冊:2005-05-26

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-06-15 15:26:09 IP:61.230.xxx.xxx 未訂閱
我用debug去看,我確定是先跑add後,才去read值得...還有Synchronize這個要怎麼用呢..
andychang1690
資深會員


發表:20
回覆:694
積分:442
註冊:2003-03-14

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-06-15 15:50:30 IP:221.169.xxx.xxx 未訂閱
liang-shien:以下是我使用Thread的範例你參考看看:
Unit1.h
#include "Msg_Thread.h"
private:        // User declarations
  MsgThread *msgthread;
Unit1.cpp
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  if (msgthread) msgthread->Terminate();
  Application->Terminate() ;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  msgthread = new MsgThread();
}
//---------------------------------------------------------------------------
Msg_Thread.cpp
#include 
#pragma hdrstop    #include "Msg_Thread.h"
#include "Unit1.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------    //   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 MsgThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------    __fastcall MsgThread::MsgThread()
  : TThread(false)
{
}
__fastcall MsgThread::~MsgThread()
{
}
//---------------------------------------------------------------------------
void __fastcall MsgThread::Execute()
{
  //---- Place thread code here ----
  FreeOnTerminate = true;
  while(!Terminated)  {
    Synchronize(fsMsgDo);
    Sleep(300);
  }
}
//---------------------------------------------------------------------------
void __fastcall MsgThread::fsMsgDo(void)
{
//  執行程式如
//  Form1->ComboBox1->items->add("123");
}
Msg_Thread.h
#ifndef Msg_ThreadH
#define Msg_ThreadH
//---------------------------------------------------------------------------
#include 
//---------------------------------------------------------------------------
class MsgThread : public TThread
{            
private:
  void __fastcall fsMsgDo(void);
protected:
  void __fastcall Execute();
public:
  __fastcall MsgThread();
  __fastcall ~MsgThread();
};
//---------------------------------------------------------------------------
#endif
以上於BCB6 OK!
請參考!
Andy Chang
------
Andy Chang
系統時間:2024-05-04 11:03:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!