Solved

Update progress bar

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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