Is this procedure running right now?

Alisa

Registered User.
Local time
Today, 13:29
Joined
Jun 8, 2007
Messages
1,868
I have a procedure that runs on a timer on a form in my program. If a user clicks a button on another form at the precise millisecond that the timer event is running, it causes problems. So, I need to either do something within the timer event to prevent any other code from running until it is done, or I need to do something within the button event to check whether the timer event is currently running and wait till it is done. Does anyone know how to do either of those things? What I need is like the opposite of doevents.

My temporary solution is to set hourglass to true while the timer event runs to prevent the user from clicking on anything, but I would rather have a solution that is invisible to the user - the hourglass flashing on and off looks sloppy to me.

I tried CurrentProject.AllModules(myModule).IsLoaded, but that always returns true even when the code within the module isn't running.

I have found a few posts with API code to determine if external processes, like excel, are running, but that doesn't help in this situation. I have also seen the shell and wait posts, but I can't see how that would apply to this situation.

Any ideas?
 
There's no way I've ever found that can check if a function is currently executing. What I've done in similar situations is have a global boolean and upon starting the process I set the value to true and when the process is done I set the value to false. Then you can just check the status of the variable and see if you are able to run.
 
One thought is setting a global variable to True when the process starts and False when it ends. You can then test that variable.
 
Well, both of you came up with the same idea, so it must be good!
Thanks guys,
Alisa
 
Great minds think alike...sometimes mine does too. :p
 
Use a static to do this:
Code:
Private Sub Form_Timer()
Static AlreadyRunning As Integer
    If AlreadyRunning Then 'Don't run the code if it's already running.
        Exit Sub
    End If
    AlreadyRunning = 1 'Make it so the code won't re-enter.
    Call DoStuff 'Perform the actual work you wanted done here.
    AlreadyRunning = 0 'Now it's safe to run the code again.
End Sub
 
Use a static to do this:
Code:
Private Sub Form_Timer()
Static AlreadyRunning As Integer
    If AlreadyRunning Then 'Don't run the code if it's already running.
        Exit Sub
    End If
    AlreadyRunning = 1 'Make it so the code won't re-enter.
    Call DoStuff 'Perform the actual work you wanted done here.
    AlreadyRunning = 0 'Now it's safe to run the code again.
End Sub

I think I need to use a global because in this case, the code that I don't want to run is in a different form than the form that the time event is in, but thank you for the suggestion.
 

Users who are viewing this thread

Back
Top Bottom