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 <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
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 <vcl.h>
#pragma hdrstop
#include "Main.h "
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
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(),
⪻ocessAffinityMask,
&SystemAffinityMask))
{
Application-> Terminate();
}
for(int i=0;i <sizeof(SystemAffinityMask)*8;i++)
{
if(SystemAffinityMask&(1 < <i))
{
dynamic_cast <TSpeedButton*> (FindComponent( "SpeedButton "+IntToStr(i+1)))-> Enabled=true;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if(!Sender-> InheritsFrom(__classid(TSpeedButton)))
{
return;
}
TSpeedButton *Button=dynamic_cast <TSpeedButton*> (Sender);
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 <ComponentCount;i++)
{
TComponent *comp=Components[i];
if(comp-> InheritsFrom(__classid(TSpeedButton)))
dynamic_cast <TSpeedButton *> (comp)-> Down=false;
}
Sleep(1000);
}
//---------------------------------------------------------------------------
//TSpeedButton的Caption分別為1~32的數字。