Link to home
Start Free TrialLog in
Avatar of turnerrob
turnerrob

asked on

Timer On Form. Problem Exiting The Form

I have a Form Say (Form3), which has a Timer on it
which executes a large amount of code on each 2 seconds.
There is a command button, (Say Command1) which , on click,
executes some code and closes the form.

My program allways crashes on hitting the command button.,
ie The Timer event does not complete its functions.

1. I have tried putting a  Tactive=true  Tactive=False
   in the timer code.. ie Tactive is Public.
   On Click of the command button i look for Tactive=False (itnever becomes false)
2. Have put DoEvents Everywhere.  NO effect.

???  MY question.

How do i click on Command1 , and have the timer finish it processes, then turn Timer off, and complete the procces.


Avatar of vbPhil
vbPhil

The below code should disable the timer, so it will not fire before you want it to...  Then manually call the Timer event to execute the code contained within.



Private Sub Command1_click()

  Timer1.Enabled = False
  Call Timer1_Timer()

End Sub


Private Sub Timer1_Timer()

  Static A as Long

  'Doing Something Here for Example

  A = A + 1
  If A > 1000 then
    A = 0
  End If


End Sub
Avatar of turnerrob

ASKER

vbphil..Appreciate your comment.
I gave this a try., but i seem to still have the problem,
the Timer function does not complete., and a variable does not get the required value.
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Richie.
Thanks for your comment, This does do the job!!!!
I have put a sleep 1 second, at the end of the timer
The program does not bomb out and all works ok.
The Button flashing on/off is a little distracting!!
Would be nice if there was a less brutal solution.
Try usinbg a global variable let's say
Dim NotNow as boolean

At the top of your timer code set NowNow to True
Set it back to False at the end of the timer code.
In the
Private Sub Command1_click()
If NotNow = True then
  ' Code to let the user know to try again a few seconds
Else
  'Your code and exit
End If
End Sub
hes.Appreciate your comment.
I have given this a go., and it did not work.,
somehow when the command button is clicked, thats the end of the timer event!!
Don't disable the command button...
Instead...
In the command button Add code something like this

Do
If terminate = True Then
    End
End If
Loop

terminate is just a publicly declared boolean
this just loops until the timer has finished...(i.e. until the timer has set the terminate flag to true...

Cheers...
Ber...
ritchie. Thanks for your comment.
This idea works, will use this until i have more time to look at timers more carefully

regards turnerrob
ber.
I have tried putting a public variable at the beginning and end of the timer event, and using it in the Command1
button.
But it does not seem to work!!, When the command1 is clicked, the timer never gets the variable set to (false)

In the timer set the terminate variable and then call the sleep function this should work...

something like this...


Private Sub Timer1_Timer()
Dim count As Integer

Do While count < 20000
count = count + 1
Loop
terminate = True
Sleep 1

End Sub

Cheers...
Ber...
ber.
Will give your idea a try when i return from a job
Not use End!
Using End statement is like to try to stop a car running to 100 mph against a wall.
Thanks for "A" grade.
Richie at the risk of pointing out the obvious, A wall usually does a good job of stopping a car (as I found out to my expense last year .... I don't recommend trying it though ;)  )

Cheers...
Ber...
yes, it does the job but how and at which cost it does is the real problem.
:))
I was messing around with this earlier using the method I suggested and it had some pretty nasty effects so I suggest that you be careful if you choose to use anything similar

Cheers...
Ber...
I see you have a solution already... but I would throw this in for you anyway.


In the Command1_Click() set a flag variable...

  bCloseWhenFinished = True
  Command1.Enabled = False
  Timer1.Enabled = False
  DoEvents
  Call Timer1_Timer()





In the Timer1_Timer event...

at the end of the proceedure,

  If bCloseWhenFinished = True Then
    Unload Me
  End If



This prevents the user from pressing Command1 Again, Allows the timer code to finish, and then closes the form after that.