Problem with Timer function
At the load of a form I need to fill some data, but in order to do it correctly the program must wait a sec or two.
So I decided to use the typical Timer sample:
VB Code:
Dim dTimer As Double
dTimer = Timer
waitTime = 1
Do While Timer < dTimer + waitTime
DoEvents
Loop
Now the problem is that if I close the program while the program's still inside the Do While the form opens again and gives an error. Any help? Thanks in advance.
Re: Problem with Timer function
Don't close the form until it is finished.
Create a boolean variable and set it to True when the process starts. Set it to False when the process ends.
If an attempt to close the form is made, check the variable. If it is still True, don't let the form unload.
Re: Problem with Timer function
Quote:
Originally Posted by Hack
Don't close the form until it is finished.
Create a boolean variable and set it to True when the process starts. Set it to False when the process ends.
If an attempt to close the form is made, check the variable. If it is still True, don't let the form unload.
So, isn't any way of exiting the loop when closing the form?
Re: Problem with Timer function
Or add a flag in the while clause
Do While (Timer < dTimer + waitTime) And yourFlag
Simply set public scope flag to false when exiting app. BTW, its probably the statements following the loop code that reshows the form.
Do While (Timer < dTimer + waitTime) And Not ForcedExitFlag
Loop
If ForcedExitFlag Then Exit Function/Sub
Re: Problem with Timer function
Quote:
Originally Posted by Neverbirth
So, isn't any way of exiting the loop when closing the form?
Yes, you can go that way if you wish but I got the impression you didn't want the process interupted.
Any, if the form is unloading then just exit the DO
Re: Problem with Timer function
Quote:
Originally Posted by leinad31
Or add a flag in the while clause
Do While (Timer < dTimer + waitTime) And yourFlag
Simply set public scope flag to false when exiting app. BTW, its probably the statements following the loop code that reshows the form.
Do While (Timer < dTimer + waitTime) And Not ForcedExitFlag
Loop
If ForcedExitFlag Then Exit Function/Sub
Already tried that, but it gaves me the error as well. It calls an instance of an object already released.
Re: Problem with Timer function
I would use a timer as shown below.
- Add a timer to your form (disable it in design mode).
- At form load set the DELAY in msec and enable the timer.
- In the timer event procedure Call fillForm()
This will delay your fillForm() as long as you want; just set the Timer1.Interval accordingly. You can EXIT the form before the timer executes without any problems.
VB Code:
Private Sub Form_Load()
Timer1.Interval = 1000 'set the DELAY in msec
Timer1.Enabled = True '1000msec from now form will be filled
End Sub
Private Sub fillForm()
Text1.Text = "Hello VB World"
'fill form here
End Sub
Private Sub Timer1_Timer()
Call fillForm
Timer1.Enabled = False 'disable timer; we want this only ONCE
End Sub
Re: Problem with Timer function
Try an Exit Do instead of an Exit Sub.