Choose in the Menu : File -New -'Thread Object'
Choose a appropriate Name for your class f.i TMYThread
Now Builder creates a new form and a new unit :
__fastcall TMyThread::TMyThread(AnsiString SourceFile,AnsiString DestFile,bool CreateSuspended)
: TThread(CreateSuspended)
{/* this is the function that is called when a new thread object of type TMyThread
is made. You can add variables to the function wich are necessary to give the
parameters for your thread, f.i if you make a thread that copies files you can give
the source file, and the destination file. CreateSuspended is the parameter that
indicates that the thread will be executed as soon as a new object is created.
This function is necessary to pass external parameters to local parameters of your
thread object. this function is only a constructor, it is called when the
object is created
Finished=false; // this must be a public variable to indicate when
// the thread is finished
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
//---- Place thread code here ----
/*
This is the function where the work is done, here you write your functions
that will run in a thread. Also do not forget to include a public variable like
Finished to indicate to your base program that the thread is finished.*/
CopyFile(SourceFile,DestFile,false);
Finished=true;
}
//---------------------------------------------------------------------------
to run the thread in your base program :
TMyThread NewThread= new TMyThread(SourceFile,DestFile,false);
/* now the thread is created and executed immediately (because of the false)
and your baseprogram does not wait for the end of it.
if you want to check for the end of the thread */
while (!NewThread->Finished)
{
sleep(20) ; // wait 20msec
Application->ProcessMessages(); // this is to continue to //refresh the screen
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.