|
|
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 |
|
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: |
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:
and gave an additional tip of:
$50.00
Great job - thanks so much!!! |
|
There are no comments at this time. |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |