March 6th, 2012, 03:35 AM
Basic multi thread example
I've been scouring the internet for a specific example but am having no luck. I doubt this is too much of a challenge for someone.
What I need is an example where the VCL passes a start and end number to a thread. The thread then loops from start to finish, and everytime it gets to a number divisible by 10, it passes back "hello world" to a listbox on the original form.
The application must still be useable during the thread loop so I can continue opening other forms.
Basic i know, but I'm having issues
Delphi version 2006
March 6th, 2012, 11:23 AM
You've mentioned that you've searched for examples, but don't know if you've read this (rather lengthy) article on how threads work in Delphi. Hopefully this will help you..
March 6th, 2012, 02:46 PM
Hi there and thanks for the reply.
Yes I have read through this tutorial and it is very good.
The issue I have is that the demo he works through processes an entire loop within the thread before passing back the full results.
What I would like it something that passes a single entry in the result list as it goes back to the VCL before continuing in its loop.
Maybe someone can alter the example he has, otherwise maybe someone can point out what part of the code I might need to change.
March 6th, 2012, 05:44 PM
First, passing values to your thread is rather simple, you just need to declare a new constructor for your thread class, and have a place to hold these values within your thread, and to update your main thread, you need a parameterless procedure you can call with synchronize:
Assuming your thread's unit is called MyThreadUnit, and your thread is called TMyThread:
Then for the form that will create your thread and needs to be updated by your thread, place a button on the form, and a memo control. Add your thread to the uses clause and add a private variable of your Thread's class
TMyThread = class(TThread)
procedure Execute; override;
constructor Create(lValue, uValue: Integer; Suspended: Boolean);
uses MainForm; //Unit of the form that will be updated by thread
constructor TMyThread.Create(lValue, uValue: Integer; Suspended: Boolean);
fLowerLimit := lValue;
fUpperLimint := uValue;
FreeOnTerminate := True;
For I := fLowerLimit to fUpperLimit do
if (I mod 10) = 0 then
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);
procedure TForm1.Button1Click(Sender: TObject);
CountingThread := TMyThread.Create(22, 148, True)
Last edited by majlumbo; March 7th, 2012 at 09:12 AM.
March 14th, 2012, 08:11 AM
The main issue with this example is that it doesn't leave the UI useable. If I run this loop to 1000, I'm stuck until it finishes.
March 14th, 2012, 09:03 AM
Originally Posted by Boicey
You're dealing with a delay because of the Synchronize method, if you want to minimize that, then another approach could be to define a user message that your application can respond to, and have your thread send that message.
You can look here on how to define a WM_USER message.