Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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.


0
turnerrob
Asked:
turnerrob
  • 6
  • 4
  • 3
  • +2
1 Solution
 
vbPhilCommented:
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
0
 
turnerrobAuthor Commented:
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.
0
 
Richie_SimonettiCommented:
why don't you disable the button until timer has finished its job?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
turnerrobAuthor Commented:
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.
0
 
hesCommented:
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
0
 
turnerrobAuthor Commented:
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!!
0
 
BerCommented:
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...
0
 
turnerrobAuthor Commented:
ritchie. Thanks for your comment.
This idea works, will use this until i have more time to look at timers more carefully

regards turnerrob
0
 
turnerrobAuthor Commented:
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)

0
 
BerCommented:
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...
0
 
turnerrobAuthor Commented:
ber.
Will give your idea a try when i return from a job
0
 
Richie_SimonettiCommented:
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.
0
 
BerCommented:
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...
0
 
Richie_SimonettiCommented:
yes, it does the job but how and at which cost it does is the real problem.
:))
0
 
BerCommented:
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...
0
 
vbPhilCommented:
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.

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now