Question Do While and No Form

solfinker

Well-known member
Joined
Aug 6, 2013
Messages
71
Programming Experience
Beginner
Easy peasy...

I have a LOOP within a LOOP, but the Form doesn't show until the end of the main LOOP:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim mo As Integer
Do While mo < 5
mo = mo + 1


Dim Start As Double = Microsoft.VisualBasic.DateAndTime.Timer
Dim Finish As Double = Start + 1.0 ' Set end time for 1-second duration.
Do While Microsoft.VisualBasic.DateAndTime.Timer < Finish
Loop


lbl1.Text = mo
Loop


End Sub


Thank you very much for your help.
 
Of course it doesn't. The Load event handler is executed after you call Show and before the form is displayed. The Load event handler doesn't end until your code is executed and the form doesn't display until the Load event handler has ended. If you want to do something immediately after the form displays then you handle Shown, not Load.

Don't do that either though because that code cannot possibly do anything good. Please explain exactly what you're trying to achieve and we will explain the proper/best way to achieve it.
 
Thank you!

I have some calculations and I want to show the result of them with a circle that changes its size along with them.
If I have - let's say - five consecutive results, I want to show them all in a sequence, not only the final result. And being the processors too fast, I need to elapse some time between the different results. That's why I use the inner Loop:


Dim Start As Double = Microsoft.VisualBasic.DateAndTime.Timer

Dim Finish As Double = Start + 1.0 ' Set end time for 1-second duration.
Do While Microsoft.VisualBasic.DateAndTime.Timer < Finish
Loop

that I suppose do nothing but pause.

What about using a second Form for showing the partial results?
 
Add a Timer to your form from the Toolbox, set its Interval appropriately and handle its Tick event. It's like an alarm clock, raising an event periodically to allow you to perform an action without freezing the UI in between.
 
I have several choices now, and I am not sure which is the best. Maybe sth like the following?

Public Class Form1
Private mo As Integer
Private Sub Form1_load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
mo = mo + 1
lbl1.Text = mo
If mo = 5 Then Timer1.Stop()


End Sub
End Class

I still don't understand very well how this works, Am I supposed to embed all the calculations - and I'm talking of about 2000 code lines - in the Timer1.Tick event?
 
Last edited:
I don't know exactly what calculations you're talking about but, as I said, a Timer is like an alarm clock. You set an alarm on the clock and it goes off every 12 hours or, on digital clocks, every 24 hours. A .NET Timer has a variable interval that can range from effectively about 50 ms up to just over 24 days. You set the Interval property to the number of milliseconds you want between events and then, when you call Start or set Enabled to True, the Timer will start raising its Tick event every time that number of milliseconds passes.

As an example, let's say that you have a list of words and you want to display them to the user in a Label one by one with a 2 second interval. You would set the Interval of the Timer to 2000, you would put the words in an array and you'd use an Integer to store the index of the word to display. In the Tick event handler you would increment that index and then display the word at the new index. Once the index went beyond the limit of the array, you would stop the Timer.

In your case, exactly where you should put the calculations depends on exactly what the calculations do. Maybe some should be done up front and maybe some need to be done every Tick. Maybe some ought to be done on a secondary thread if they are time consuming.
 
Again with my Loop:
If I code sth like

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
mo = mo + 1
End Sub


and Timer1 is enabled to True, mo is supposed to increase every interval value (msecs). But in the following code, mo remains 0.

Public Class Form1
Private mo As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Do While mo < 5

Timer1.Enabled = True
Timer1.Start()

Loop
Timer1.Enabled = False
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
mo = mo + 1
End Sub

End Class

I'm still missing sth.

Thanks for your help.
 
Get rid of the loop. No loop! The purpose of a loop is to do something multiple times. That is now being achieved by the Timer, which raises its Tick event multiple times. You have to do something useful in the Tick event handler. The ONLY thing you do in the Load event handler is Start the Timer. Everything else, including stopping the Timer, is done in the Tick event. Have you even considered my alarm clock analogy?
 
And I want to do sth multiple times. So the only way to time control them is to perform all the calculations within the tick event.
Thank you very much for your help.
 
Back
Top