1)自定义消息ID
#define WM_MYMESSAGE WM_APP+1
2)消息接收处理函数
void __fastcall CMMessage(TMessage &msg);
void __fastcall TActFrm::CMMessage(TMessage &msg)
{
g_logFile.print("receive msg.\n");
if(msg.WParam == 0){
}else if(msg.WParam == 1){
}
}
3)消息交给CMMessage 函数处理
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_MYMESSAGE,TMessage,CMMessage)
//决定WM_MYMESSAGE消息交给CMMessage 函数处理
END_MESSAGE_MAP(TActiveForm)
4)在多线程文件中头文件如下
ifndef ThreadH
#define ThreadH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TMyThread : public TThread
{
private:
LPVOID m_handle;
protected:
void __fastcall Execute();
void __fastcall FtpDwonLoad(); //自定义函数
public:
__fastcall TMyThread(bool CreateSuspended,LPVOID handle);
void __fastcall DwonLoad(char *sourceFile,char* strFile); //DownFile
};
#endif
5)多线程的cpp文件如下
__fastcall TMyThread::TMyThread(bool CreateSuspended,LPVOID handle)
: TThread(CreateSuspended)
{
m_handle = handle;
}
void __fastcall TMyThread::Execute()
{
DwonLoad(gDown,gListen);
PostMessage(m_handle,WM_MYMESSAGE,0,0);
}
void __fastcall TMyThread::DwonLoad(char *sourceFile,char* strFile)
{
}
6)多线程的使用:
先定义 TMyThread *thread1;
初始化: thread1=new TMyThread(true,Handle);
后启动:thread1->Resume();
最后停止: thread1->Terminate();