Hi there
[quote]Here’s how I’d handle it:
Create a Task subclass designed to do this export. It will contain all the information is needs to complete its task. That can include an array of files to export.
When the user clicks export, store this information in the Task’s properties, disable the interface, then run the Task.
React to the Task’s events to update your progress bar. When it tells you it’s done, re-enable the interface.
At no time do you create a loop to monitor the Task, you let it feed you the information instead.
If you don’t want to handle disabling the interface, move this function to a dialog window and have it do the processing in the way I described. When the Task is finished, it will close the dialog.[/quote]
Thanks so much to everyone for their help with this. I feel I am slowly getting the picture. As Kem said, my experience is with pretty linear programming requirements. Never really used threads much, not even to update progress bars!
Can I just clarify a couple of points pls?
-
What is being suggested is pretty much going to put the entire code-base for my export process in the Run event of the Task. Feels wrong to me to not be able to make the code more modular and break it up more. Given I am in a Method of the Task Class (Run), what can I safely access from a ‘Scope’ perspective ? (I read it is limited). Can I access Globals? Can I access Form level Methods & Properties? Or does everything have to be self-contained in the Run method of the task?
-
Reacting to the Tasks events. So are you saying that I create an event in the Task sub-class that gets raised when the export has finished? And in that event put the code to re-enable the UI? More new territory Not created my own events before…
-
So working in this way presumably means I must not have any code that linearly follows the call to Task.Run, otherwise it will get run when the Thread surrenders time back to the main app? So basically, in my Go button, one line, Task.Run (if we exclude all the property setting up stuff to initialise values in the Task). After that nothing.
-
The updating of the UI (the point of all this headache!) is still achieved in the Task using the same UpdateUI method that is in the Xojo example Task code?
Sorry to be a pain about this, as you say, it is a relatively new way of thinking for me. I am used to events in a single thread sense, but the interaction between the main app and a separate thread, with events between the two is a bit of a challenge right now. But thank you for hanging in with me
Regards
Mark