Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make a thread

Threads

How to make a thread

by  wimvanherp  Posted    (Edited  )
Threads in C++ BUILDER

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
}



Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top