Results 1 to 8 of 8

Thread: Problem with Timer function

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    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:
    1. Dim dTimer As Double
    2.             dTimer = Timer
    3.             waitTime = 1
    4.             Do While Timer < dTimer + waitTime
    5.                 DoEvents
    6.             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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    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?

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    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.

  7. #7
    New Member PeterHof's Avatar
    Join Date
    Feb 2007
    Location
    Annapolis, MD, USA
    Posts
    4

    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:
    1. Private Sub Form_Load()
    2.     Timer1.Interval = 1000   'set the DELAY in msec
    3.     Timer1.Enabled = True    '1000msec from now form will be filled
    4. End Sub
    5.  
    6. Private Sub fillForm()
    7.     Text1.Text = "Hello VB World"
    8.     'fill form here
    9. End Sub
    10.  
    11. Private Sub Timer1_Timer()
    12.     Call fillForm
    13.     Timer1.Enabled = False  'disable timer; we want this only ONCE
    14. End Sub

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Problem with Timer function

    Try an Exit Do instead of an Exit Sub.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width