Any way to prevent timer event from being interrupted by other code?

  • Thread starter HONOREDANCESTOR
  • Start date
H

HONOREDANCESTOR

I have a timer which goes off every second and executes some code. I
also have a menu item that sets some variables to NOTHING. The
problem is that the timer uses some of those variables. If I execute
the menu item while the timer is firing an event, I get a crash.
So I need to make the menu item wait for a timer event to be finished
before the menu item executes its own code. Is there a way to do this
- perhaps with 'synclock' on some variable that the two routines have
in common?
Thanks,
HA
 
R

rowe_newsgroups

I have a timer which goes off every second and executes some code. I
also have a menu item that sets some variables to NOTHING. The
problem is that the timer uses some of those variables. If I execute
the menu item while the timer is firing an event, I get a crash.
So I need to make the menu item wait for a timer event to be finished
before the menu item executes its own code. Is there a way to do this
- perhaps with 'synclock' on some variable that the two routines have
in common?
Thanks,
HA

Does the Timer have to have instantiated variables to do it's job? In
other words could you do either if...then tests or use a try...catch
block to allow the Timer to continue no matter what?

I don't think synclock will help here, as it will only prevent
multiple objects from grabbing it at the same time (mostly a problem
with multithreaded solutions). The problem is that the menu item is
grabbing the variables before the timer has a chance to execute. In
this case a simple boolean variable could be used to allow/deny access
to the variables. Initialize the bool variable to false, and set it to
true when the timer finishes executing. Then the menuitem would be
able to test this bool and determine whether or not it should set the
variables to nothing.

Thanks,

Seth Rowe
 
P

Patrice

In addition to "rome_newsgroups" it's IMO always good to explain what you
are trying to do (so that someone could perhaps suggest a better approach
for what you are trying to do, for now it looks unusual to me having a menu
and a timer competing for the same elements).
 
H

HONOREDANCESTOR

The idea of checking a boolean to tell whether the timer event is over
leaves me with a question.
First I should explain my code a little more.
I have a timer that uses a database library, and so uses a class that
allows it to write to that database. I don't want to re-create that
class every time I use the timer, since it takes time to create it and
since it is used by other parts of the program.
I also have a "File/Exit" menu item. This sets the 'class' variable
to NOTHING. The problem arises in the following situation.
1. The Timer event fires, and it takes 2 seconds or more to execute
its code.
2. During those 2 seconds, the user clicks on FILE/EXIT, which sets
the class to NOTHING. It also sets the 'enabled' flag of the timer
to False.
3. The timer event tries to access the class, but the class no longer
exists
4. The timer event is over.
Now I could have a "intimer" variable, so that FILE/EXIT would do
something like this:
do While (intimer)
DoEvents
loop
Timer1.Enabled = False
Set myclass = Nothing
Would this work?
Thanks.
HA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top