寻
寻路
Unregistered / Unconfirmed
GUEST, unregistred user!
我写代码如下:
头文件BInfer.h
//---------------------------------------------------------------------------
class TDBThread : public TThread {
private:
TADOConnection *AdoCn;
TADOQuery * AdoQu;
public:
static const WM_TABLE_OPEN = (WM_USER + 100);
private:
//打开表
void __fastcall WM_OPEN(TMessage &Message);
//通过宏定义实现消息的正确响应
begin
_MESSAGE_MAP
MESSAGE_HANDLER(WM_TABLE_OPEN, TMessage, WM_OPEN)
END_MESSAGE_MAP(TDBThread)
protected:
void __fastcall Execute();
public:
__fastcall TDBThread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
class TDBInfer {
private:
TDBThread *DBThread;
public:
TDBInfer();
~TDBInfer();
public:
bool Open(const String TableName);
};
//---------------------------------------------------------------------------
#endif
实现部分BInfer.cpp
//---------------------------------------------------------------------------
__fastcall TDBThread::TDBThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
FreeOnTerminate = true;
}
//---------------------------------------------------------------------------
void __fastcall TDBThread::Execute()
{
PMsg Msg;
PMessage DMsg;
while(!Terminated) {
Sleep(10);
//释放CPU资源
if(PeekMessage(Msg, 0, 0, 0, PM_REMOVE)) {
DMsg->Msg = Msg->message;
DMsg->WParam = Msg->wParam;
DMsg->LParam = Msg->lParam;
DMsg->Result = 0;
Dispatch(DMsg);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TDBThread::WM_OPEN(TMessage &Message)
{
MessageBox(0, "OK", "TEST", 0);
}
//---------------------------------------------------------------------------
/*
数据库类
*/
TDBInfer::TDBInfer()
{
//创建一个线程对象
try {
DBThread = new TDBThread(false);
}
catch ( ... ) {
delete DBThread;
DBThread = NULL;
}
}
TDBInfer::~TDBInfer()
{
}
bool TDBInfer::Open(const String TableName)
{
if(PostThreadMessage(DBThread->ThreadID, DBThread->WM_TABLE_OPEN, 0, 0) == 0) {
MessageBox(0, "PostThreadMessage ERROR", "Test", 0);
}
return 0;
}
我在程序中调用如下:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DBInfer = new TDBInfer(DBInfer);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
DBInfer->Open("");
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
delete DBInfer;
}
现在我已经实现了消息在线程中的传递,问题是程序在退出的时候报指针错误!
不知道是什么原因,请大家帮忙看看!
头文件BInfer.h
//---------------------------------------------------------------------------
class TDBThread : public TThread {
private:
TADOConnection *AdoCn;
TADOQuery * AdoQu;
public:
static const WM_TABLE_OPEN = (WM_USER + 100);
private:
//打开表
void __fastcall WM_OPEN(TMessage &Message);
//通过宏定义实现消息的正确响应
begin
_MESSAGE_MAP
MESSAGE_HANDLER(WM_TABLE_OPEN, TMessage, WM_OPEN)
END_MESSAGE_MAP(TDBThread)
protected:
void __fastcall Execute();
public:
__fastcall TDBThread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
class TDBInfer {
private:
TDBThread *DBThread;
public:
TDBInfer();
~TDBInfer();
public:
bool Open(const String TableName);
};
//---------------------------------------------------------------------------
#endif
实现部分BInfer.cpp
//---------------------------------------------------------------------------
__fastcall TDBThread::TDBThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
FreeOnTerminate = true;
}
//---------------------------------------------------------------------------
void __fastcall TDBThread::Execute()
{
PMsg Msg;
PMessage DMsg;
while(!Terminated) {
Sleep(10);
//释放CPU资源
if(PeekMessage(Msg, 0, 0, 0, PM_REMOVE)) {
DMsg->Msg = Msg->message;
DMsg->WParam = Msg->wParam;
DMsg->LParam = Msg->lParam;
DMsg->Result = 0;
Dispatch(DMsg);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TDBThread::WM_OPEN(TMessage &Message)
{
MessageBox(0, "OK", "TEST", 0);
}
//---------------------------------------------------------------------------
/*
数据库类
*/
TDBInfer::TDBInfer()
{
//创建一个线程对象
try {
DBThread = new TDBThread(false);
}
catch ( ... ) {
delete DBThread;
DBThread = NULL;
}
}
TDBInfer::~TDBInfer()
{
}
bool TDBInfer::Open(const String TableName)
{
if(PostThreadMessage(DBThread->ThreadID, DBThread->WM_TABLE_OPEN, 0, 0) == 0) {
MessageBox(0, "PostThreadMessage ERROR", "Test", 0);
}
return 0;
}
我在程序中调用如下:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DBInfer = new TDBInfer(DBInfer);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
DBInfer->Open("");
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
delete DBInfer;
}
现在我已经实现了消息在线程中的传递,问题是程序在退出的时候报指针错误!
不知道是什么原因,请大家帮忙看看!