Solved

Update progress bar

Posted on 2009-07-01
2
401 Views
Last Modified: 2013-11-23
I have a form and an alert dialog which contain a progress bar.
How to update it from the form? If i want to update it also from
a thread should I create a specific progressbar class in a way that
is able to handle postmessage from the thread?
0
Comment
Question by:jaja2005
  • 2
2 Comments
 
LVL 4

Expert Comment

by:JonasMalmsten
ID: 24756038
Just reference the form and the progressbar, like

Form2.ProgressBar1.Position := ...;

If this doesn't make sense, how do you create the alert form?

In almost all cases I would say that a thread should not be aware of the progressbar (or any other graphical components). It is usually better to isolate GUI components from data processing components. One way to do this is to create an event property in your thread class, see TNotifyEvent and the TThread.Synchronize method. The form containing the progressbar can then hook the event and update its own progressbar. If you tell me a little more about what your thread is doing maybe I can suggest a better solution.
0
 
LVL 4

Accepted Solution

by:
JonasMalmsten earned 500 total points
ID: 24756795
I just read your other questions... is there a reason why you don't want to use Synchronize?

The key thing here is to conform to the Model-View pattern. Consider the thread being your Model and the form being your View. By declaring a TNotifyEvent (or any other custom event) in your thread class your Model will not know anything about your View, and the View can use its knowledge about the Model to update itself.

Using events would be the standard delphi way for doing this but you can accomplish almost the same thing by having the thread send a windows message to the main form. The message should then be just a notification to the main form that something in the thread class has changed. For example that partial work was completed. The main form can the use properties of functions declared in the thread to update the progressbar and any other GUI components. Think about it this way, what if one day you want to replace the progressbar to something different or maybe use 2 progressbars. This should not require you to change any code inside your thread.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now