Real Software Forums

Progress Bar in Windows?
Page 1 of 1

Author:  jjfcpa [ Tue Apr 30, 2013 1:47 pm ]
Post subject:  Progress Bar in Windows?

I have a progress bar this is working perfectly in OSX, but does not show up in Windows.

It may be my coding, but I'm hoping it something else. Here is the way it works...

1. Call a Window

2. Window in #1 calls the progress bar window and passes it a parameter that tells it what method to call in Window called in #1

3. Progress Bar windows calls method in Window called in #1

This allows me to have a generic progress bar that I can call from any Window. Works great in OSX, but as I stated, in Windows, the code still runs but the progress bar does not show.

If you want a copy of the progress bar window, I'd be happy to send to you.

Author:  timhare [ Tue Apr 30, 2013 2:51 pm ]
Post subject:  Re: Progress Bar in Windows?

The standard answer here is, "Put the method call into a thread." On Windows, the progressbar (as well as the rest of the UI) relies on being serviced by the "event loop", which runs in the main thread. Any other code running in the main thread causes the event loop to be delayed until that code finishes. So you see no UI updates during that time. Putting the code into a thread allows the event loop to share time with that code.

Author:  jjfcpa [ Tue Apr 30, 2013 8:14 pm ]
Post subject:  Re: Progress Bar in Windows?


Very frustrating ...

I hope you can set me straight.

I have a form with a timer and thread on it, and of course, a progress bar.

In the startup method, I call a ShowProgress method which has the following code in it.

PBValue = 0
PBMax = 1000
PBUpdate = True

The first 3 items are properties of the form.

In the run method of the thread I have the following code.

self.Timer1.Mode = timer.ModeMultiple

The method ScanAssets is where are the processing takes place. In this method, I update the properties PBValue, PBMax, and PBUpdate in a loop that runs.

Finally, in the Timer.Action, I have the following code.

if Thread1.State <> 4 then
if PBUpdate then
pb1.Maximum = PBMax
pb1.Value = PBValue
PBUpdate = False
self.Timer1.Mode = 0
pb1.Value = pb1.Maximum

What appears to be happening is that the progressbar displays and runs with a sort of oscillating display. It definitely is not updating and I can tell that the timer.action is not running. I have the timer period set to 1000 and almost 5000 records that are being processed. Even if I change the timer period to 10000, the display is not updating. From what I can tell, the timer.action is only being run when the ScanAssets() method ends.

Any help would be appreciated.

Author:  jjfcpa [ Tue Apr 30, 2013 8:28 pm ]
Post subject:  Re: Progress Bar in Windows?


Does it matter if the code in the ScanAssets() updates some controls (text boxes) on the window? I was reading that the code in the thread can not update any controls and since I do update some controls is that what's causing the progressbar to not update properly?

Author:  timhare [ Tue Apr 30, 2013 10:24 pm ]
Post subject:  Re: Progress Bar in Windows?

You shouldn't update controls from the thread. But that won't block the progressbar. More likely your thread isn't yielding enough time to the UI. This can take a little trial and error to get the right balance of throughput and smooth display updating.

Either reduce the priority of the thread, or put in an occasional app.YieldNextThread call. If you go with yielding, do it every X milliseconds, not every time through the loop. Updating the display a few times per second is enough to produce a pleasing progress indicator.

Author:  jjfcpa [ Wed May 01, 2013 7:57 am ]
Post subject:  Re: Progress Bar in Windows?


Thanks for your help. As usual you provide the solution I was looking for. By putting the app.yieldtonextthread in my loop, I now have a progressbar that is updating properly. I will need to tweak it a bit because I don't want to slow things down too much, which is what is happening now. I'm going to put in some code to only do this every few hundred records.

One other complication is that my app allows the user to create an unlimited number of data files - it's an accounting application and they will be creating files for their clients. The files may be on a local drive or a network drive and if it's on a network, I probably don't want to slow it down too much because the network will do that for me.

Nevertheless, you've got me going in the right direction and I greatly appreciate it.

One other thing I just might mention. I originally had my progress bar on a sheet window in OSX and just assumed that it would show up as a modal window in Windows. However, that wasn't the case ... it didn't show up at all. Do you think it's due to the same reason that the thread was not allowing the UI to update?

Author:  timhare [ Wed May 01, 2013 12:15 pm ]
Post subject:  Re: Progress Bar in Windows?

I'd recommend using either Ticks or Microseconds to determine when to yield, rather than the number of records.

Author:  jjfcpa [ Wed May 01, 2013 3:38 pm ]
Post subject:  Re: Progress Bar in Windows?

That's a good idea then it isn't affected by the speed of the computer...

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group