方法一 原始CreateThread
h文件----
class TForm1 : public TForm
{
。。。
};
extern PACKAGE TForm1 *Form1;
DWORD WINAPI MyFun ( LPVOID lParam );
#endif
cpp文件-
DWORD WINAPI MyFun ( LPVOID lParam )
{
Sleep(5000);
Form1->Caption = Now();
}
void __fastcall TForm1::Button4Click(TObject *Sender)
{
//三句话调用线程
HANDLE hThread;
hThread = CreateThread ( NULL, 0, MyFun, NULL, 0, NULL );
CloseHandle(hThread);
}
方法二 封装简化
头文件声明
void funAPIThread(FuntionThreadDef pfun);
cpp文件定义
void funAPIThread(FuntionThreadDef pfun)
{
HANDLE hThread;
hThread = CreateThread ( NULL, 0, pfun, NULL, 0, NULL );
CloseHandle(hThread);
}
void __fastcall TForm1::Button5Click(TObject *Sender)
{
//封装成函数,简化成一句话
funAPIThread(MyFun);//MyFun所有任意函数名,无参
}