Synchronizing Threads and GUI in A Delphi Application
Synchronizing Threads and GUI in A Delphi Application
by Zarko Gajic
Updated January 28, 2019
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 1/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
Multi-threading in Delphi lets you create applications that include several simultaneous
paths of execution.
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 2/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
A normal Delphi application is single-threaded, which means all VCL objects access their
properties and execute their methods within this single thread. To speed up data processing
in your application, include one or more secondary threads.
Processor Threads
Advertisement
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 3/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
To update your application's user interface, or main thread, from a secondary thread, you
need to call the Synchronize method. This technique is a thread-safe method that avoids
multi-threading conflicts that can arise from accessing object properties or methods that are
not thread-safe, or using resources not in the main thread of execution.
Below is an example demo that uses several buttons with progress bars, each progress bar
displaying the current "state" of the thread execution.
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 4/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
unit MainU;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, ComCtrls, StdCtrls, ExtCtrls;
type
//interceptor class
TButton = class(StdCtrls.TButton)
OwnedThread: TThread;
ProgressBar: TProgressBar;
end;
TMyThread = class(TThread)
private
FCounter: Integer;
FCountTo: Integer;
FProgressBar: TProgressBar;
FOwnerButton: TButton;
procedure DoProgress;
procedure SetCountTo(const Value: Integer) ;
procedure SetProgressBar(const Value: TProgressBar) ;
procedure SetOwnerButton(const Value: TButton) ;
protected
procedure Execute; override;
public
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 5/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
Interval = 1000000;
begin
FreeOnTerminate := True;
FProgressBar.Max := FCountTo div Interval;
FProgressBar.Step := FProgressBar.Max;
while FCounter < FCountTo do
begin
if FCounter mod Interval = 0 then Synchronize(DoProgress) ;
Inc(FCounter) ;
end;
FOwnerButton.Caption := 'Start';
FOwnerButton.OwnedThread := nil;
FProgressBar.Position := FProgressBar.Max;
end;
procedure TMyThread.SetCountTo(const Value: Integer) ;
begin
FCountTo := Value;
end;
procedure TMyThread.SetOwnerButton(const Value: TButton) ;
begin
FOwnerButton := Value;
end;
procedure TMyThread.SetProgressBar(const Value: TProgressBar) ;
begin
FProgressBar := Value;
end;
procedure TMainForm.Button1Click(Sender: TObject) ;
var
aButton: TButton;
aThread: TMyThread;
aProgressBar: TProgressBar;
begin
aButton := TButton(Sender) ;
if not Assigned(aButton.OwnedThread) then
Advertisement
begin
aThread := TMyThread.Create(True) ;
aButton.OwnedThread := aThread;
aProgressBar := TProgressBar(FindComponent(StringReplace(aButton.Name,
'Button', 'ProgressBar', []))) ;
aThread.ProgressBar := aProgressBar;
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 7/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
aThread.OwnerButton := aButton;
aThread.Resume;
aButton.Caption := 'Pause';
end
else
begin
if aButton.OwnedThread.Suspended then
aButton.OwnedThread.Resume
else
aButton.OwnedThread.Suspend;
aButton.Caption := 'Run';
end;
end;
end.
Advertisement
Ad
Ad
Advertisement
Delphi Team
Delphi Team
Ad closed by Sie suchen eine Firma zur Betreuung
Ihrer Delphi/SQL Anwendung?
Sie suchen eine Firma zur Betreuung
Stop seeing this
Ihrer Delphi/SQL Anwendung? ad
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 8/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
CITE
Continue Reading
Article Article
Delphi Thread Pool Example Using Creating a Delphi Notepad: Open
AsyncCalls and Save
Article Article
Multithreaded Delphi Database How to Dynamically Add Items to a
Queries With dbGo (ADO) TPopUp Delphi Menu
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 9/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
Article
Multi-Threading in C# Using the
Task Parallel Library in .NET 4.0
Article
How to De ne and Implement
Interfaces in Delphi
Article
Article Delphi Resource Files Made Easy
How to Show a Login Before the
Main Form in Delphi
Article
Working with GIF images in Delphi
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 10/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
Article
Use the Right Coding to Display Article
Menu Item Hints in Delphi How to Place a Checkbox Into a
Delphi DBGrid
Article
How to Place a TProgressBar Into a
TStatusBar in Delphi
Article
Using a Function as a Parameter in
Another Function Article
How to Implement OnCreate Event
for Delphi TFrame Object
Article
How to Run Delphi Applications
With Parameters
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 11/13
17/10/2019 Synchronizing Threads and GUI in a Delphi application
Article
Learn How to Con gure and Edit
.INI Files in Delphi
Follow Us
TRUSTe
Languages Resources
About Us Advertise
https://www.thoughtco.com/synchronizing-threads-and-gui-delphi-application-1058159 13/13