I can’t figure out how to display a box with a progress bar to track the loading of a large disk file.
The file is a text file residing on the host computer and being read by a Xojo app on the host. It takes several minutes to read the whole file. I have a progressBar and I’m trying to update its value as the app gets the file contents.
Along with the text input stream, look at opening the file on another thread, and use a timer to check the progress / update the progress bar without locking up the main thread. This way you can also support a cancel button and the user interface remains responsive, windows does not stop responding or Mac show beach ball.
Both the thread and timer can be the GUI control versions as well to make coding easier.
Graham, thanks so much for your response. I think I can do this although threads are unfamiliar territory.
It’s not clear to me how to open the file on another thread. All the rest seems easy enough. Would you please point me to examples or other help on implementing threads? Many thanks.
I’m not near my computer at the moment but generally a simple solution would be to add a thread to your window as a gui control. Add a property to store the progress to the window. Then implement the run event in your thread. All code run in this event will be run on another thread. So add your file open logic and update your progress property as you go along. You cannot directly update your progress bar so that’s where the timer comes in.
To start the thread call thread1.run() and turn on your timer. You can add some other property’s to keep track of errors and running state, or look at thread.state ( I think that’s the property)
To support cancel add a cancelled Boolean to the window and have the thread look at that and a button to set the cancelled Boolean to true. Then in your thread look at the cancelled flag and stop and close the file.
Returning out of the thread will end it.
All of this is just a guide, I am sure there are examples of threading in the xojo examples folder. Wrapping your file open in a class that either subclasses or has a thread in it might also work better for your application.
Also remember your application will not wait for the thread to end after calling threads.run , so you will need to disable interface to ensure users don’t close windows etc while you are opening the file.
Still having trouble. It seems that Threads have been updated but the examples have not been. A new event << UserInterfaceUpdate(data() as Dictionary) >> has been added.
It would be nice if someone could show a very simple example of loading a file and showing the progress of the load with a progressBar.
Any help will be gratefully appreciated.
If someone would be kind enough to remind me how to post a code snippet here, I will upload what I have been trying…
It sounds like you want this example, which has been updated to use the new event:
Thanks… I’ll take a look right away. I am running 2019 Release 2.1. Does that matter?
The example is also updated in 2019r2.1 so it should not matter.
OK… the example compiles and runs. Hopefully it will help me see the light.