0% found this document useful (0 votes)
618 views

Synchronizing Threads and GUI in Delphi Application

The document discusses synchronizing threads and the GUI in Delphi applications. It describes how to update the GUI (main thread) from secondary threads using the Synchronize method to avoid conflicts. An example app uses buttons and progress bars, where clicking a button starts a thread that updates its associated progress bar. The thread execution is suspended and resumed by clicking the button to pause/resume the progress.

Uploaded by

Helio Ferreira
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
618 views

Synchronizing Threads and GUI in Delphi Application

The document discusses synchronizing threads and the GUI in Delphi applications. It describes how to update the GUI (main thread) from secondary threads using the Synchronize method to avoid conflicts. An example app uses buttons and progress bars, where clicking a button starts a thread that updates its associated progress bar. The thread execution is suspended and resumed by clicking the button to pause/resume the progress.

Uploaded by

Helio Ferreira
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

17/06/13

Synchronizing Threads and GUI in Delphi application

POP3 IMAP4 Component


www.emailarchitect.net POP3, IMAP4, MIME component for ASP, C#, VB, VBA, VC , .Net
C om puting

Delphi
Synchronizing Threads and GUI in Delphi application
From Zarko Gajic, former About.com Guide
C ode subm itte d by Je ns Borrisholt

Share

Discuss in our forum

Multi-threading in Delphi lets you create applications that include several simultaneous paths of execution. A "normal" Delphi application is single-threaded: all (VCL) objects access their properties and execute their methods within this single thread. Threads and GUI To speed up data processing in your application you can decide to include one or more "secondary" threads.

Synchronizing Thre ads and GUI

When several threads are running in the application a question arises: how to update your GUI (user interface) as a result of a thread execution. The question lies in the TThread class Synchronize method. To update your application's user interface - main thread - from a secondary thread you need to call the Synchronize method - 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. An example demo uses several buttons with progress bar, each progress bar displaying the current "state" of the thread execution... Download source code u n i tM a i n U ; i n t e r f a c e u s e s W i n d o w s ,M e s s a g e s ,S y s U t i l s ,V a r i a n t s ,C l a s s e s ,G r a p h i c s ,C o n t r o l s ,F o r m s , D i a l o g s ,C o m C t r l s ,S t d C t r l s ,E x t C t r l s ; t y p e / / i n t e r c e p t o rc l a s s T B u t t o n=c l a s s ( S t d C t r l s . T B u t t o n ) O w n e d T h r e a d :T T h r e a d ; P r o g r e s s B a r :T P r o g r e s s B a r ; e n d ; T M y T h r e a d=c l a s s ( T T h r e a d ) p r i v a t e F C o u n t e r :I n t e g e r ; F C o u n t T o :I n t e g e r ; F P r o g r e s s B a r :T P r o g r e s s B a r ; F O w n e r B u t t o n :T B u t t o n ; p r o c e d u r eD o P r o g r e s s ; p r o c e d u r eS e t C o u n t T o ( c o n s tV a l u e :I n t e g e r ); p r o c e d u r eS e t P r o g r e s s B a r ( c o n s tV a l u e :T P r o g r e s s B a r ); p r o c e d u r eS e t O w n e r B u t t o n ( c o n s tV a l u e :T B u t t o n ); p r o t e c t e d p r o c e d u r eE x e c u t e ;o v e r r i d e ; p u b l i c c o n s t r u c t o rC r e a t e ( C r e a t e S u s p e n d e d :B o o l e a n ); p r o p e r t yC o u n t T o :I n t e g e rr e a dF C o u n t T ow r i t eS e t C o u n t T o ; p r o p e r t yP r o g r e s s B a r :T P r o g r e s s B a rr e a dF P r o g r e s s B a rw r i t eS e t P r o g r e s s B a r ; p r o p e r t yO w n e r B u t t o n :T B u t t o nr e a dF O w n e r B u t t o nw r i t eS e t O w n e r B u t t o n ; e n d ; T M a i n F o r m=c l a s s ( T F o r m ) B u t t o n 1 :T B u t t o n ; P r o g r e s s B a r 1 :T P r o g r e s s B a r ; B u t t o n 2 :T B u t t o n ;

delphi.about.com/od/kbthread/a/thread-gui.htm

1/3

17/06/13
P r o g r e s s B a r 2 :T P r o g r e s s B a r ; B u t t o n 3 :T B u t t o n ; P r o g r e s s B a r 3 :T P r o g r e s s B a r ; B u t t o n 4 :T B u t t o n ; P r o g r e s s B a r 4 :T P r o g r e s s B a r ; B u t t o n 5 :T B u t t o n ; P r o g r e s s B a r 5 :T P r o g r e s s B a r ;

Synchronizing Threads and GUI in Delphi application

p r o c e d u r eB u t t o n 1 C l i c k ( S e n d e r :T O b j e c t ); e n d ; v a r M a i n F o r m :T M a i n F o r m ; i m p l e m e n t a t i o n { $ R* . d f m } {T M y T h r e a d} c o n s t r u c t o rT M y T h r e a d . C r e a t e ( C r e a t e S u s p e n d e d :B o o l e a n ); b e g i n i n h e r i t e d ; F C o u n t e r: =0 ; F C o u n t T o: =M A X I N T ; e n d ; p r o c e d u r eT M y T h r e a d . D o P r o g r e s s ; v a r P c t D o n e :E x t e n d e d ; b e g i n P c t D o n e: =( F C o u n t e r/F C o u n t T o ); F P r o g r e s s B a r . P o s i t i o n: =R o u n d ( F P r o g r e s s B a r . S t e p*P c t D o n e ); F O w n e r B u t t o n . C a p t i o n: =F o r m a t F l o a t ( ' 0 . 0 0% ' ,P c t D o n e*1 0 0 ); e n d ; p r o c e d u r eT M y T h r e a d . E x e c u t e ; c o n s t I n t e r v a l=1 0 0 0 0 0 0 ; b e g i n F r e e O n T e r m i n a t e: =T r u e ; F P r o g r e s s B a r . M a x: =F C o u n t T od i vI n t e r v a l ; F P r o g r e s s B a r . S t e p: =F P r o g r e s s B a r . M a x ; w h i l eF C o u n t e r<F C o u n t T od o b e g i n i fF C o u n t e rm o dI n t e r v a l=0t h e nS y n c h r o n i z e ( D o P r o g r e s s ); I n c ( F C o u n t e r ); e n d ; F O w n e r B u t t o n . C a p t i o n: =' S t a r t ' ; F O w n e r B u t t o n . O w n e d T h r e a d: =n i l ; F P r o g r e s s B a r . P o s i t i o n: =F P r o g r e s s B a r . M a x ; e n d ; p r o c e d u r eT M y T h r e a d . S e t C o u n t T o ( c o n s tV a l u e :I n t e g e r ); b e g i n F C o u n t T o: =V a l u e ; e n d ; p r o c e d u r eT M y T h r e a d . S e t O w n e r B u t t o n ( c o n s tV a l u e :T B u t t o n ); b e g i n F O w n e r B u t t o n: =V a l u e ; e n d ; p r o c e d u r eT M y T h r e a d . S e t P r o g r e s s B a r ( c o n s tV a l u e :T P r o g r e s s B a r ); b e g i n F P r o g r e s s B a r: =V a l u e ; e n d ; p r o c e d u r eT M a i n F o r m . B u t t o n 1 C l i c k ( S e n d e r :T O b j e c t ); v a r a B u t t o n :T B u t t o n ; a T h r e a d :T M y T h r e a d ; a P r o g r e s s B a r :T P r o g r e s s B a r ; b e g i n a B u t t o n: =T B u t t o n ( S e n d e r ); i fn o tA s s i g n e d ( a B u t t o n . O w n e d T h r e a d )t h e n b e g i n

delphi.about.com/od/kbthread/a/thread-gui.htm

2/3

17/06/13
a T h r e a d: =T M y T h r e a d . C r e a t e ( T r u e ); a B u t t o n . O w n e d T h r e a d: =a T h r e a d ;

Synchronizing Threads and GUI in Delphi application

a P r o g r e s s B a r: =T P r o g r e s s B a r ( F i n d C o m p o n e n t ( S t r i n g R e p l a c e ( a B u t t o n . N a m e ,' B u t t o n ' ,' P r o g r e s s B a r ' ,[ ] ) ) ); a T h r e a d . P r o g r e s s B a r: =a P r o g r e s s B a r ; a T h r e a d . O w n e r B u t t o n: =a B u t t o n ; a T h r e a d . R e s u m e ; a B u t t o n . C a p t i o n: =' P a u s e ' ; e n d e l s e b e g i n i fa B u t t o n . O w n e d T h r e a d . S u s p e n d e dt h e n a B u t t o n . O w n e d T h r e a d . R e s u m e e l s e a B u t t o n . O w n e d T h r e a d . S u s p e n d ; a B u t t o n . C a p t i o n: =' R u n ' ; e n d ; e n d ; e n d .

Top Related Searches Thread Execution

Delphi Application

Example Demo

Graphics Controls

Windows Messages

Single Thread

delphi.about.com/od/kbthread/a/thread-gui.htm

3/3

You might also like