sobota, 23 września 2017

Example class Thread (C++ Builder)

#include <vcl.h>
#include <math.h>
using namespace std;


bool is_First(int x)
{
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
if(x%i==0)
 ++sum;
if(sum==2)
return true;
else
return false;
}

void write_ListBox(TListBox *LB, int value)
{
LB->Items->Add(IntToStr(value));
}

SECURITY_ATTRIBUTES this_attributes={sizeof(SECURITY_ATTRIBUTES),
    NULL,TRUE};



class example_Thread
{
private:
TListBox *listB;
LONG run_T;


HANDLE my_Event;
public:
example_Thread(TListBox *tempBox);
~example_Thread();
UINT my_ID;
int  next_First(LPVOID values);
void start_Thread();
void suspend_Thread();
void resume_Thread();
};

example_Thread::example_Thread(TListBox *tempBox)
{
run_T=0;
listB=tempBox;

}
example_Thread::~example_Thread()
{
CloseHandle((LPVOID)run_T);
}
int example_Thread::next_First(LPVOID values)
{
int x;
x=1;
for(;;)
{
  if(is_First(x))
write_ListBox(listB,x);
  WaitForSingleObject((LPVOID)run_T,500);
  ++x;
}
}
void example_Thread::start_Thread()
{

run_T=BeginThread(&this_attributes,4096,next_First,this,CREATE_SUSPENDED,my_ID);
if(run_T==(int)INVALID_HANDLE_VALUE)
{
ShowMessage("ERROR THREADS!!!");
}
else
{
ResumeThread((LPVOID)run_T);
my_Event=CreateEvent(NULL,FALSE,FALSE,NULL);
if(my_Event)
{
WaitForSingleObject(my_Event,500);
CloseHandle(my_Event);
}
}
}
void example_Thread::suspend_Thread()
{
SuspendThread((LPVOID)run_T);
}
void example_Thread::resume_Thread()
{
ResumeThread((LPVOID)run_T);
}

Brak komentarzy:

Prześlij komentarz