Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Thread Sample Delphi

I am new with this stuff of Threading in Delphi. so, I am trying to make a simple query aplication that make a bit call up for the database and take a bit of time, so I want to alert the user that there is a background process and have to be patient.

I tried many samples, but none of them work for me, Please, could somebody show me a simple sample that could work?

I know that I have to Declare a Type of TThread, with Create and Override Execute... etc.. but since that I am lost...

Using Delphi 7, SQL Server 2005 and ADO, Windows XP sp3.-

Thanks.

like image 606
Jose M. Vilomar Avatar asked Aug 10 '10 16:08

Jose M. Vilomar


People also ask

How do I use thread in Delphi?

Multi-threading in Delphi lets you create applications that include several simultaneous paths of execution. A normal Delphi application is single-threaded, which means all VCL objects access their properties and execute their methods within this single thread.


1 Answers

Yup, you declare a new type which inherits from TThread:

TMyWorkerThread = class(TThread) end; 

Then you add a function override for Execute():

TMyWorkerThread = class(TThread) public   procedure Execute; override; end; 

That procedure will be called when you start your thread. It will be executed in parallel with your main program. Let's write it.

procedure TMyWorkerThread.Execute; begin   //Here we do work    DoSomeWork();    DoMoreWork();   //When we exit the procedure, the thread ends.   //So we don't exit until we're done. end; 

How to use this? Let's say you want to start doing work when the user clicks button. You write an OnClick handler:

procedure TMainForm.Button1Click(Sender: TObject); begin   TMyWorkerThread.Create(false); end; 

That's it. After the user clicks button, your thread starts and proceeds with doing whatever it is that you wrote in Execute. If the user clicks the button again, another thread will start, and then another - one every click. They will all run in parallel, each doing all what's written in Execute() and then ending.

Let's say you want to check if the work is over. For that, you'll have to store the reference to your thread somewhere:

TMainForm = class(TForm) {...skipped...} public   MyWorkerThread: TThread; end;  procedure TMainForm.Button1Click(Sender: TObject); begin   //This time we make sure only one thread can be started.   //If one thread have been started already, we don't start another.   if MyWorkerThread<>nil then     raise Exception.Create('One thread have already been started!');   MyWorkerThread := TMyWorkerThread.Create(false); end;  procedure TMainForm.Button2Click(Sender: TObject); begin   //If the work is not over yet, we display message informing the user we're still working   if (MyWorkerThread<>nil) and (WaitForSingleObject(MyWorkerThread.Handle, 0)<>WAIT_OBJECT_0) then     MessageBox(Self.Handle, pchar("The work is not yet done!"), pchar("Still running"), MB_OK); end; 

As you see, we're checking if a thread is still running by calling a Windows function called WaitForSingleObject. This function waits until the thread is done working, or the timeout is elapsed, and as we specify the timeout of 0, it just exists immediately if the thread is not over yet.

like image 161
himself Avatar answered Sep 21 '22 04:09

himself