Progress bar in thread questions

I’ll preface this by saying that I’ve looked at the example projects, the video, and the older video on this subject. I’ve decided to use the Task Class method of updating a progress bar window. However, I’m having issues with it. I think the best way for me to explain what I’m doing is to walk through the flow:

[code]* User presses Start button on Main Window, chooses a folderitem
** folderitem is put in a global property (App.TheFolder)
** the Progress Bar window is opened (UIProgressWindow.Show)
** UIProgressWindow.Task1.Run is called from Start button
*** UIProgressWindow.Task1.run calls a method in the main window,
which iterates through App.TheFolder recursively, generating a list of files to process.
*** As this is running, it adds key/value pairs to a dictionary at certain
steps, which it passes to UIProgressWindow.Task1.UpdateUI

  • Code in the start button initiates the processing of files once the list is finished
    [/code]
    The slowest part of the code is in iterating through the folder, as some of the subfolders could contain 100k or more files. In a typical setup, there could be a 15-30 second delay, based on my testing. So I want a progress bar to at least indicate that something is happening, and ideally to show the file it’s currently looking at.

The problem I’m having is that even though the progress bar window is called up before the processing begins, the progress bar itself doesn’t appear until after the most time consuming task is complete, and then it just shows a full progress bar and the name of the last file in the set that it processed. Not particularly helpful to the user.

I must be missing something obvious here, because I’m doing all the stuff outlined in the documentation and the videos:

-putting the code that does the work inside the thread (via a method called from Thread.Run)
-Using Thread.UpdateUI to refresh the progress bar from within that same method

Is there something else that I’m not doing right in the basic setup?

Not every function is thread-friendly. FolderItem functions are blocking even when threaded. To provide the window manager time to draw the progress window, delay the start of the thread until the progress window has opened.

Spaghetti is for dinner, not for code. I would try to design the thread to be entirely self-contained for best results.

The ideal process for this in my opinion would be:

  1. User presses Start button on Main Window and chooses a folder item
  2. FolderItem gets put as a property on an instance of your processing thread subclass
  3. Show progress window
  4. Start processing thread (a property or interface object on the progress window)
  5. Upon completion, get any necessary results off the thread instance.

You could pass a thread object instance to the progress window, or have the object instance already on the progress window and set it up. The first can lead itself to a more reusable progress window.

I have modified the threading / task example to show how this would be achieved using the lazier thread as an interface object approach: UIThreadingWithTask.xojo_binary_project

Thanks. Adding a small delay fixed the window not opening part.

And on a hunch, I also added a delay into the processing loop. Sure enough, the progress bar started to update.

There will be a fair bit of cleanup here once I’m done. Just getting stuff working for now.

The Observer Design Pattern helps you?