kkkor
一般會員
發表:7 回覆:7 積分:2 註冊:2005-03-12
發送簡訊給我
|
第一次練習執行緒 一直抓不到錯在哪
不知道為什麼那一行會一直出錯 導致後面沒辦法呼叫執行緒
|
Fm
初階會員
發表:19 回覆:66 積分:37 註冊:2003-10-15
發送簡訊給我
|
kkkor你好
試著把標頭檔"TimerThread.h"丟到"main.h"去
//新手上路,請多包涵。
/*青青子矜,悠悠我心。但為君故,沈吟至今。*/
|
kkkor
一般會員
發表:7 回覆:7 積分:2 註冊:2005-03-12
發送簡訊給我
|
引言:
kkkor你好
試著把標頭檔"TimerThread.h"丟到"main.h"去
//新手上路,請多包涵。
/*青青子矜,悠悠我心。但為君故,沈吟至今。*/
是在 main.h 中加入 #include "TimerThread.h" 嗎?
還是一樣出錯 =o="
|
Fm
初階會員
發表:19 回覆:66 積分:37 註冊:2003-10-15
發送簡訊給我
|
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
發送簡訊給我
|
放到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
發送簡訊給我
|
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
發送簡訊給我
|
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
發送簡訊給我
|
kkkor你好 錯誤訊息已經說了,他看不懂TimerThread是什麼
所以你要在main.cpp中先宣告TTimerThread *TimerThread;
|
Jiahong1012
一般會員
發表:7 回覆:17 積分:9 註冊:2004-11-05
發送簡訊給我
|
KKKor,
把true 改成 false 試試看
TimerThread = new TTimerThread(false)
|
kkkor
一般會員
發表:7 回覆:7 積分:2 註冊:2005-03-12
發送簡訊給我
|
引言:
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
發送簡訊給我
|
引言:
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;
}; //新手上路,請多包涵。
/*青青子矜,悠悠我心。但為君故,沈吟至今。*/
非常感謝 我還以為我這行有加了 原來是忘掉這行了
|