Solved

Timer On Form. Problem Exiting The Form

Posted on 2002-07-23
16
168 Views
Last Modified: 2010-05-02
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
Comment
Question by:turnerrob
  • 6
  • 4
  • 3
  • +2
16 Comments
 
LVL 2

Expert Comment

by:vbPhil
ID: 7172931
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
 

Author Comment

by:turnerrob
ID: 7172947
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
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 50 total points
ID: 7172954
why don't you disable the button until timer has finished its job?
0
 

Author Comment

by:turnerrob
ID: 7172963
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
 
LVL 20

Expert Comment

by:hes
ID: 7173006
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
 

Author Comment

by:turnerrob
ID: 7173019
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
 
LVL 2

Expert Comment

by:Ber
ID: 7173020
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
 

Author Comment

by:turnerrob
ID: 7173023
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:turnerrob
ID: 7173032
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
 
LVL 2

Expert Comment

by:Ber
ID: 7173044
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
 

Author Comment

by:turnerrob
ID: 7173068
ber.
Will give your idea a try when i return from a job
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7174080
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
 
LVL 2

Expert Comment

by:Ber
ID: 7175587
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7175776
yes, it does the job but how and at which cost it does is the real problem.
:))
0
 
LVL 2

Expert Comment

by:Ber
ID: 7175941
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
 
LVL 2

Expert Comment

by:vbPhil
ID: 7177943
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now