Google Answers Logo
View Question
 
Q: VB Code to load second form so that second form's timer event's always fire? ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: VB Code to load second form so that second form's timer event's always fire?
Category: Computers > Programming
Asked by: crom654-ga
List Price: $200.00
Posted: 05 Sep 2003 11:52 PDT
Expires: 05 Oct 2003 11:52 PDT
Question ID: 252648
Using VB6, I need a way to call an existing form AND HAVE THAT SECOND
FORM'S teimer events go off in the midst of a while loop (on the first
form...). I would expect this code to start in form1, show the number
of variable x, then show that form2 is loaded, then show that the
timer1 on form2 has fired, but no dice. If you can show me how
something like this would work that would be great.

Basically, I have a form that does exactly what I want it to but it
only acts on 1 file. I need it to act on a directory full of files,
and make sure that the timers go off because they are needed to make
sure the file is sent as a fax. I know I can use the wait function to
simulate this functionality, but I don't want to use it because the
designers of the preesisting form tell me that timers are the best way
to go, plus, I'm not a very good programmer and don't have a lot of
time.
Everyone tells me the doevents should accomplish what I want, but they
just don't.


Thanks.


In Form1 on_load event put code like this:
dim x as integer
x=1
while x<10
doevents
msgbox x
Form2.show
load Form2
doevents
x=x+1
wend

****

in Form2, have a timer called Timer1, and put this code:

msgbox "in timer"

****

In Form2, in on_load event, have code that says:
msgbox "in Form2"
Timer1.interval=50
Timer1.enabled=true

Clarification of Question by crom654-ga on 05 Sep 2003 11:56 PDT
If there is no other way, can this be accomplished by shelling, or is that stupid?

I'll give a $50 tip if this can be answered by 9/7/3.
Answer  
Subject: Re: VB Code to load second form so that second form's timer event's always fire?
Answered By: mmastrac-ga on 06 Sep 2003 08:00 PDT
Rated:5 out of 5 stars
 
Hi crom654!

The way that I am reading your question goes like this:

1.  You have an existing form that performs an action on one specific
file.
2.  You want to run that form on a number of files.
3.  For each file, you want to ensure that the form's timer has fired
before you go on to the next form.

If I am reading your question correctly, the answer is fairly
straightforward.  The reason your timer events are not necessarily
firing is that immediately after you show your form in your loop, the
loop continues to run the next file.  It doesn't wait for the form at
all!  When it tries to show the next form, this next form "clobbers"
the existing form and the timer events get lost.

The way to solve this problem is to "talk" with the form after you've
shown it, to see when it has finished its work.  By finished its work,
I mean that the timer has fired.

I have taken your sample code and modified it slightly.  This code
requires the exact same form setup that your example does.  Two forms:
Form1 and Form2.  Form2 has a Timer named Timer1.

What this code does is display the second form and use a flag
("Finished") on this form to determine when it can go on to the next
item.  It checks this flag in the other "While" loop that I've added
before.  After each time it checks the flag, it waits 50 milliseconds.
 If you don't wait 50 milliseconds, the CPU will be pinned at 100%
just checking the flag!  We also call "DoEvents" in this loop so that
Form2 can get its timer events.

After the flag becomes "True", Form1 knows that it can continue on. 
It then hides the form and calls "Unload".  It is necessary to Unload
the second form so that it can be initialized properly the next time
through.

Form1 code
--- 8< -------------------------------------------------------------------
' Import a kernel function so that we can sleep properly
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As
Long)

Private Sub Form_Load()

Dim x As Integer
x = 1
While x < 10
    DoEvents
    MsgBox x
    
    ' Set the flag to finished before we show it
    Form2.Finished = False
    Load Form2
    Form2.Show
    
    ' Check for the "Finished" flag
    While Not Form2.Finished
        DoEvents
        Sleep (50)
    Wend

    ' Hide the form and unload it    
    Form2.Hide
    Unload Form2
    
    x = x + 1
Wend

End Sub
--- 8< -------------------------------------------------------------------


Form2 code
--- 8< -------------------------------------------------------------------
Public Finished As Boolean

Private Sub Form_Load()
    MsgBox "in Form2"
    Timer1.Interval = 50
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    MsgBox "in timer"

    ' Disable the timer
    Timer1.Enabled = False
    ' Set the "Finished" flag
    Finished = True
End Sub
--- 8< -------------------------------------------------------------------

Let me know if I have misinterpreted your question, or if you have any
additional questions.

Matt.
crom654-ga rated this answer:5 out of 5 stars and gave an additional tip of: $50.00
Great job - thanks so much!!!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy