13,824
社区成员
发帖
与我相关
我的任务
分享
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <syncobjs.hpp>
#include "Unit2.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
TMyThread *th[3];
public: // User declarations
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
extern TEvent *event[3];
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TEvent *event[3];
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
th[0]=new TMyThread(false,0,Edit1);
th[1]=new TMyThread(false,1,Edit2);
th[2]=new TMyThread(false,2,Edit3);
for(int i=0;i<3;i++)
event[i]=new TEvent(NULL,true,false,NULL);
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
for(int i=0;i<3;i++)
th[i]->Terminate();
Sleep(100);
for(int i=0;i<3;i++)
delete event[i];
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int i=0;i<3;i++)
event[i]->SetEvent();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TMyThread : public TThread
{
private:
int id;
TEdit *edt;
AnsiString str;
protected:
void __fastcall Execute();
void __fastcall updateText();
public:
__fastcall TMyThread(bool CreateSuspended,int id,TEdit *edt);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include "Unit1.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TMyThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TMyThread::TMyThread(bool CreateSuspended,int id,TEdit *edt)
: TThread(CreateSuspended)
{
this->id=id;
this->edt=edt;
FreeOnTerminate=true;
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
//---- Place thread code here ----
int i=0;
while(!Terminated)
{
if(event[id]->WaitFor(INFINITE)==wrSignaled)
{
event[id]->ResetEvent();
str=IntToStr(i++);
Synchronize(updateText);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::updateText()
{
edt->Text=str;
}
//---------------------------------------------------------------------------
DWORD WINAPI _TestProc(PVOID pvParam)
{
TEvent **evt = (TEvent **)pvParam;
for(int i=0;i<3;i++)
{
evt[i]->SetEvent();
}
return 0;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HANDLE hThread = CreateThread(NULL, 0, _TestProc,PVOID(event), 0, NULL);
CloseHandle(hThread);
}