#include <vcl.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
#include <windows.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
HANDLE hh=NULL;
UINT id;
LONG thr=0;
int m_Date[1000];
SECURITY_ATTRIBUTES s_a={sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};
bool __fastcall TForm1::is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
void __fastcall TForm1::init_Date()
{
int i,j,k;
i=0;
j=1;
do
{
if(is_First(j))
{
m_Date[i]=j;
++i;
}
++j;
}while(i<1000);
}
int __fastcall random_Values(LPVOID value)
{
int i,j;
for(;;)
{
i=rand()%1000;
j=m_Date[i];
Form1->ListBox1->Items->Add(IntToStr(i+1)+") - "+IntToStr(j));
WaitForSingleObject((LPVOID)thr,1000);
}
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
srand(time(NULL));
init_Date();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Button1->Caption="&Start";
Button2->Caption="&Suspend";
Button3->Caption="&Resume";
Button4->Caption="&Terminate";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
thr=BeginThread(&s_a,4096,random_Values,this,CREATE_SUSPENDED,id);
if(thr==(int)INVALID_HANDLE_VALUE)
{
ShowMessage("ERROR! ");
}
else
{
ResumeThread((LPVOID)thr);
hh=CreateEvent(NULL,FALSE,FALSE,NULL);
if(hh)
{
WaitForSingleObject(hh,1000);
CloseHandle(hh);
Button1->Enabled=FALSE;
hh=NULL;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
CloseHandle((LPVOID)hh);
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(hh)
SuspendThread(hh);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(hh)
ResumeThread(hh);
}