更新时间:2023-12-30 23:18
LPTHREAD_START_ROUTINE是一种函数,该函数指向一个函数,通知宿主某个线程已开始执行。
LPTHREAD_START_ROUTINE函数指针
.NET Framework 4 版 中已弃用此函数指针。
typedef DWORD (__stdcall *LPTHREAD_START_ROUTINE) ( [in] LPVOID lpThreadParameter);
lpThreadParameter
[in] 一个指向已经开始执行的代码的指针。
LPTHREAD_START_ROUTINE 指向的函数是回调函数,并且必须由承载应用程序的编写器实现。
API::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)UpdateThread,(LPVOID)ThreadPara,CREATE_SUSPENDED,&ThreadId);
//开始一个计算线程 来创建线程
以下是部分代码(在一个循环里创建线程):
DWORD ThreadId;
while(threadcount> =threadcounts)
{
Sleep(50);
Form1-> Edit3-> Text =AnsiString(Close_Pass);
Form1-> Label3-> Caption=IntToStr(threadcount);
Application-> ProcessMessages();
}
int t=ThreadPara-> emun_i;
strcpy(Close_Pass,ThreadPara-> emun_str[ThreadPara-> emun_i-1]);
HANDLE m_hHandle;
m_hHandle=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)UpdateThread,(LPVOID)ThreadPara,CREATE_SUSPENDED,&ThreadId);//开始一个计算线程
SetThreadPriority(m_hHandle,THREAD_PRIORITY_HIGHEST);
ResumeThread(m_hHandle);
//---------------------------------------------------------------------------
#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TCPUTester:public TThread
{
private:
TSpeedButton *FButton;
void __fastcall Execute(void);
public:
__fastcall TCPUTester(TSpeedButton *Button,
bool CreateSuspended=true,
unsigned long CPUMark=1);
__fastcall ~TCPUTester(void);
};
class TForm1 : public TForm
{
__published: // IDE-managed Components
TSpeedButton *SpeedButton1;
TSpeedButton *SpeedButton2;
TSpeedButton *SpeedButton3;
TSpeedButton *SpeedButton4;
TSpeedButton *SpeedButton5;
TSpeedButton *SpeedButton6;
TSpeedButton *SpeedButton7;
TSpeedButton *SpeedButton8;
TSpeedButton *SpeedButton9;
TSpeedButton *SpeedButton10;
TSpeedButton *SpeedButton11;
TSpeedButton *SpeedButton12;
TSpeedButton *SpeedButton13;
TSpeedButton *SpeedButton14;
TSpeedButton *SpeedButton15;
TSpeedButton *SpeedButton16;
TSpeedButton *SpeedButton17;
TSpeedButton *SpeedButton18;
TSpeedButton *SpeedButton19;
TSpeedButton *SpeedButton20;
TSpeedButton *SpeedButton21;
TSpeedButton *SpeedButton22;
TSpeedButton *SpeedButton23;
TSpeedButton *SpeedButton24;
TSpeedButton *SpeedButton25;
TSpeedButton *SpeedButton26;
TSpeedButton *SpeedButton27;
TSpeedButton *SpeedButton28;
TSpeedButton *SpeedButton29;
TSpeedButton *SpeedButton30;
TSpeedButton *SpeedButton31;
TSpeedButton *SpeedButton32;
void __fastcall SpeedButton1Click(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
/************/
/* CPP单元 */
/************/
//---------------------------------------------------------------------------
#include
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma package(smart_init)
TForm1 *Form1;
void __fastcall TCPUTester::Execute(void)
{
while((!this-> Terminated)&&(FButton-> Down));
}
__fastcall TCPUTester::TCPUTester(TSpeedButton *Button,
bool CreateSuspended,
unsigned long CPUMark)
:FButton(Button), TThread(CreateSuspended)
{
this-> FreeOnTerminate=true;
SetThreadAffinityMask ((void *)this-> Handle,1 < <(CPUMark-1));
}
__fastcall TCPUTester::~TCPUTester(void)
{
FButton=NULL;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DWORD ProcessAffinityMask;
DWORD SystemAffinityMask;
if(!GetProcessAffinityMask( GetCurrentProcess(),
&ProcessAffinityMask,
&SystemAffinityMask))
{
Application-> Terminate();
}
for(int i=0;i { if(SystemAffinityMask&(1 < { } } } //--------------------------------------------------------------------------- void __fastcall TForm1::SpeedButton1Click(TObject *Sender) { if(!Sender-> InheritsFrom(__classid(TSpeedButton))) { return; } TSpeedButton *Button=dynamic_cast if(NULL==Button) return; if(!Button-> Down) { return; } TCPUTester *CPUTester=new TCPUTester(Button,true,StrToInt(Button-> Caption)); if(NULL==CPUTester) { return; } CPUTester-> Resume(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { for(int i=0;i { TComponent *comp=Components[i]; if(comp-> InheritsFrom(__classid(TSpeedButton))) dynamic_cast } Sleep(1000); } //--------------------------------------------------------------------------- //TSpeedButton的Caption分别为1~32的数字。 1、《MSDN》 2、书库亚洲(shuku asia)编程频道