Solved

Update progress bar

Posted on 2009-07-01
2
400 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

8 Experts available now in Live!

Get 1:1 Help Now