線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1100
推到 Plurk!
推到 Facebook!

執行緒出錯?

尚未結案
kkkor
一般會員


發表:7
回覆:7
積分:2
註冊:2005-03-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-06 20:54:00 IP:59.104.xxx.xxx 未訂閱
第一次練習執行緒 一直抓不到錯在哪 不知道為什麼那一行會一直出錯  導致後面沒辦法呼叫執行緒
Fm
初階會員


發表:19
回覆:66
積分:37
註冊:2003-10-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-04-06 21:45:31 IP:61.62.xxx.xxx 未訂閱
kkkor你好
試著把標頭檔"TimerThread.h"丟到"main.h"去
//新手上路,請多包涵。
/*青青子矜,悠悠我心。但為君故,沈吟至今。*/
kkkor
一般會員


發表:7
回覆:7
積分:2
註冊:2005-03-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-04-06 22:11:37 IP:59.104.xxx.xxx 未訂閱
引言: kkkor你好
試著把標頭檔"TimerThread.h"丟到"main.h"去
//新手上路,請多包涵。
/*青青子矜,悠悠我心。但為君故,沈吟至今。*/
是在 main.h 中加入 #include "TimerThread.h" 嗎? 還是一樣出錯 =o="
Fm
初階會員


發表:19
回覆:66
積分:37
註冊:2003-10-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-04-06 22:36:35 IP:61.62.xxx.xxx 未訂閱
kkkor 你好..
是喔!!還是錯誤.......真是殘念
但是我猜測你的意思,寫了一個小程式如下:參考看看
///****************main.cpp
//---------------------------------------------------------------------------    #include 
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TimerThread=new TTimerThread(true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TimerThread->Resume();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    TimerThread->Suspend();
}
//---------------------------------------------------------------------------
//********************main.h
//---------------------------------------------------------------------------    #ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include "TimerThread.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
    TMemo *Memo1;
    TButton *Button1;
    TButton *Button2;
    TButton *Button3;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button3Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
private:        // User declarations
public:                // User declarations
    __fastcall TForm1(TComponent* Owner);
    TTimerThread *TimerThread;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//*************Thread部分 TTimerThread.cpp
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "TimerThread.h"
#pragma package(smart_init)
#include "main.h"
//---------------------------------------------------------------------------    //   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 TTimerThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------    __fastcall TTimerThread::TTimerThread(bool CreateSuspended)
    : TThread(CreateSuspended)
{
    FreeOnTerminate=TRUE;
}
//---------------------------------------------------------------------------
void __fastcall TTimerThread::Execute()
{
    //---- Place thread code here ----
    while(!Terminated)
    {         Synchronize(SetName);
    }
}
//---------------------------------------------------------------------------
void __fastcall TTimerThread::SetName()
{
    Form1->Memo1->Lines->Add("test!!!!!!");    }
//---------------------------------------------------------------------------
//*************TimerThread.h
//---------------------------------------------------------------------------    #ifndef TimerThreadH
#define TimerThreadH    //---------------------------------------------------------------------------
#include     //---------------------------------------------------------------------------
class TTimerThread : public TThread
{
private:
protected:
    void __fastcall Execute();
    void __fastcall SetName();
public:
    __fastcall TTimerThread(bool CreateSuspended);    };
//---------------------------------------------------------------------------
#endif
還是你把錯誤碼在po上來看看是哪裡的問題 < class="code">//新手上路,請多包涵。 /*青青子矜,悠悠我心。但為君故,沈吟至今。*/
kkkor
一般會員


發表:7
回覆:7
積分:2
註冊:2005-03-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-04-07 06:29:12 IP:59.104.xxx.xxx 未訂閱
放到main.h後我改成這樣 還是在一樣的地方出問題 
main.ccp
//---------------------------------------------------------------------------    #include 
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CGAUGES"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------    void __fastcall TForm1::FormCreate(TObject *Sender)
{
 TimerThread = new TTimerThread(true);
}
//---------------------------------------------------------------------------    main.h
//---------------------------------------------------------------------------    #ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include "TimerThread.h"
#include "CGAUGES.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
        TCGauge *CGauge1;
        TButton *Button1;
        TButton *Button2;
        TButton *Button3;
        void __fastcall FormCreate(TObject *Sender);
private:        // User declarations
public:                // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif    TimerThread.ccp
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "TimerThread.h"
#include "main.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 TTimerThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------    __fastcall TTimerThread::TTimerThread(bool CreateSuspended)
        : TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void TTimerThread::SetName()
{
        THREADNAME_INFO info;
        info.dwType = 0x1000;
        info.szName = "TimerThread";
        info.dwThreadID = -1;
        info.dwFlags = 0;            __try
        {
                 RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD),(DWORD*)&info );
        }
        __except (EXCEPTION_CONTINUE_EXECUTION)
        {
        }
}
//---------------------------------------------------------------------------
void __fastcall TTimerThread::Execute()
{
        SetName();
        //---- Place thread code here ----
        while (1)
        {
          Sleep(100);
          Form1->CGauge1->Progress =           ((Form1->CGauge1->Progress   1) % Form1->CGauge1->MaxValue);
        }
}
//---------------------------------------------------------------------------    TimerThread.h
//---------------------------------------------------------------------------    #ifndef TimerThreadH
#define TimerThreadH
//---------------------------------------------------------------------------
#include 
//---------------------------------------------------------------------------
class TTimerThread : public TThread
{
  typedef struct tagTHREADNAME_INFO
  {
    DWORD dwType;     // must be 0x1000
    LPCSTR szName;    // pointer to name (in user addr space)
    DWORD dwThreadID; // thread ID (-1=caller thread)
    DWORD dwFlags;    // reserved for future use, must be zero
  } THREADNAME_INFO;
private:
  void SetName();
protected:
        void __fastcall Execute();
public:
        __fastcall TTimerThread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
發表人 - kkkor 於 2005/04/07 06:31:46 發表人 - kkkor 於 2005/04/07 16:35:45
Fm
初階會員


發表:19
回覆:66
積分:37
註冊:2003-10-15

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-04-07 08:43:36 IP:59.120.xxx.xxx 未訂閱
kkkor你好
試著在TForm class下加入這各data member
class TForm1 : public TForm
{
__published:        // IDE-managed Components
    TMemo *Memo1;
    TButton *Button1;
    TButton *Button2;
    TButton *Button3;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button3Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
private:        // User declarations
public:                // User declarations
    __fastcall TForm1(TComponent* Owner);
    TTimerThread *TimerThread;
};    
//新手上路,請多包涵。
/*青青子矜,悠悠我心。但為君故,沈吟至今。*/
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-04-07 09:07:18 IP:210.68.xxx.xxx 未訂閱
kkkor您好: PO程式碼的方式請參考版規說明,煩請修改謝謝您的配合 < href="http://delphi.ktop.com.tw/topic.php?TOPIC_ID=48259">http://delphi.ktop.com.tw/topic.php?TOPIC_ID=48259
blk5743
高階會員


發表:34
回覆:371
積分:236
註冊:2003-11-17

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-04-07 09:10:49 IP:61.66.xxx.xxx 未訂閱
kkkor你好 錯誤訊息已經說了,他看不懂TimerThread是什麼 所以你要在main.cpp中先宣告TTimerThread *TimerThread;
Jiahong1012
一般會員


發表:7
回覆:17
積分:9
註冊:2004-11-05

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-04-07 11:52:28 IP:203.75.xxx.xxx 未訂閱
KKKor, 把true 改成 false 試試看 TimerThread = new TTimerThread(false)
kkkor
一般會員


發表:7
回覆:7
積分:2
註冊:2005-03-12

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-04-07 16:39:27 IP:59.104.xxx.xxx 未訂閱
引言: kkkor您好: PO程式碼的方式請參考版規說明,煩請修改謝謝您的配合 < href="http://delphi.ktop.com.tw/topic.php?TOPIC_ID=48259">http://delphi.ktop.com.tw/topic.php?TOPIC_ID=48259
以改正 多謝版主指導
kkkor
一般會員


發表:7
回覆:7
積分:2
註冊:2005-03-12

發送簡訊給我
#11 引用回覆 回覆 發表時間:2005-04-07 16:50:08 IP:59.104.xxx.xxx 未訂閱
引言: kkkor你好
試著在TForm class下加入這各data member
class TForm1 : public TForm
{
__published:        // IDE-managed Components
    TMemo *Memo1;
    TButton *Button1;
    TButton *Button2;
    TButton *Button3;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button3Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
private:        // User declarations
public:                // User declarations
    __fastcall TForm1(TComponent* Owner);
    TTimerThread *TimerThread;
};    
//新手上路,請多包涵。
/*青青子矜,悠悠我心。但為君故,沈吟至今。*/
非常感謝 我還以為我這行有加了 原來是忘掉這行了
系統時間:2024-05-19 15:23:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!