I have an application that performs various tasks that can take up to a minute each. I wanted to make sure that I was setting the application to a "working status" in a consistent way, so I set up a class (we'll call it "LockI") that when initialized records the current state of various things (the cursor, the status bar, screen updating and so on). Then when it's restrict method is called, it sets everything to a working status. In the class destructor all of the original settings are restored, so I don't have to worry about an error (or someone forgetting to call the restore method) leaving the application all locked up. Also because it restore the application to the state it was found in, if a second call to the class is made, I don't have to worry about it accidentally unlocking the application when the class deeper in the stack is destroyed.
So now to my question, assuming that what I have described above isn't already a mistake, should the application be locked inside the various procedures, or lock the application in the event handlers (like a button click handler) and then call the procedure? (Or some other option.)
Clarification: To be more concise... Taking into account the following:
- For longer actions, you will need an instance of the Lock class available in order to update the progress bar.
- You might be calling more than one procedure (unless you funnel them through a top-level procedure).
- You won't be displaying a status bar for all procedures (as sometimes it's just not needed and actually slows things down).
Is it better to instantiate the class in the click handler and then pass it to the called procedure(s) as needed or to instantiate the class in the called procedure and if you need to call multiple procedures set up a "funnel"?