多线程如何使用ADOQuery啊?
Lersh 2002-05-31 04:31:24 源程序如下:(省略了ConnectionString)
#include <vcl.h>
#pragma hdrstop
#include "Test.h"
#include "MyThread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
MyThread *ADOThread;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall MyThread::Execute()
{
TADOQuery *ADO=new TADOQuery(NULL);
ADO->ConnectionString="...";
ADO->SQL->Text="Select TOP 100 * from lttitle";
ADO->Open();
Form1->Memo1->Lines->Clear();
while(!ADO->Eof)
{
Form1->Memo1->Lines->Add(ADO->FieldByName("title")->AsString);
ADO->Next();
} //---- Place thread code here ----
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ADOThread=new MyThread(true);
ADOThread->FreeOnTerminate=true;
}
MyThread.cpp
#ifndef MythreadH
#define MythreadH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class MyThread : public TThread
{
private:
protected:
void __fastcall Execute();
public:
__fastcall MyThread(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif
MyThread.cpp
#include <vcl.h>
#pragma hdrstop
#include "Mythread.h"
#pragma package(smart_init)
__fastcall MyThread::MyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
FreeOnTerminate=true;
Resume();
}
//---------------------------------------------------------------------------
这个程序只要一点Button2就会显示“标记没有引用储存”,这是怎么回事啊?