vb.net IF Statement Loop

I am attempting to loop an If Statement so that if a certain condition is true, it will continuously loop through the if statement until the condition is false.  When I attempt to run it with the code below, the loop doesn't happen, no errors, just doesn't happen:
Label4.text is a number that's predetermined, but I manually supply it to test the below code

Dim test As String = Label4.Text
        If test > "0" Then
            Do While True
                Dim idelay As Integer = 1 * 1000
                Me.Timer1.Interval = idelay 
                Me.Timer1.Enabled = True
                delay(2000)
                If Me.Label2.Text <= "118985" Then
                    Label6.Visible = True
                    Me.Size = New Size(1015, 392)
                    Button1.Enabled = True
                    Button1.Text = "Test!"
                    Button1.ForeColor = Color.Red
                    Button1.Location = New Point(663, 21)
                    Me.Timer1.Enabled = False
                    Me.Label1.Text = "You"
                    Me.Label2.Text = "Did"
                    Me.Label3.Text = "It, WOO-HOO PARTY TIME!!!"
                Else
                    Label6.Visible = False
                    Button1.Enabled = False
                End If
            Loop
        End If

Open in new window

derek7467Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
The following compares strings
If Me.Label2.Text <= "118985" Then
which means that "2" is GREATER then "118985".  Is that what you want?

I think you need to convert to integer values
if CInt(Me.Label2.Text) <= 118985 then
0
derek7467Author Commented:
Label2.text has a minutes value in it.  Not sure 2 being greater than 118985 has anything to do with it.
0
derek7467Author Commented:
also, if I remove the loop, the if statement fires correctly.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

AndyAinscowFreelance programmer / ConsultantCommented:
>>Label2.text has a minutes value in it.

It could contain cheese - just make certain you understand comparing two strings does NOT have the same effect as comparing two numbers (or two time values....)


>>also, if I remove the loop, the if statement fires correctly.
I have the feeling your code is working but you are stuck in a permanent loop giving the impression it does not work.

does something like this make a difference

Dim test As String = Label4.Text
dim ContinueLoop as boolean = true
        If test > "0" Then
            Do While ContinueLoop
                Dim idelay As Integer = 1 * 1000
                Me.Timer1.Interval = idelay
                Me.Timer1.Enabled = True
                delay(2000)
                If Me.Label2.Text <= "118985" Then
                    Label6.Visible = True
                    Me.Size = New Size(1015, 392)
                    Button1.Enabled = True
                    Button1.Text = "Test!"
                    Button1.ForeColor = Color.Red
                    Button1.Location = New Point(663, 21)
                    Me.Timer1.Enabled = False
                    Me.Label1.Text = "You"
                    Me.Label2.Text = "Did"
                    Me.Label3.Text = "It, WOO-HOO PARTY TIME!!!"
ContinueLoop = false
                Else
                    Label6.Visible = False
                    Button1.Enabled = False
                End If
            Loop
        End If
0
draekCommented:
No, your code is not running, it is just blocking the UI and run forever because you don't have a break in the while loop and you don't cast the label value as an int and also you don't increment it.So, if you adapt your code like this, should work.


Dim x As Integer = 25
		If test > "0" Then
			Do While True
				Dim idelay As Integer = 1 * 1000
				System.Threading.Thread.Sleep(100)
				If x <= CInt("100") Then 'bad practice though 
					Me.Size = New Size(1015, 392)
					Button1.Enabled = True
					Button1.Text = "Test!"
					Button1.ForeColor = Color.Red
					Button1.Location = New Point(663, 21)
					Me.Label1.Text = "You"
					Me.Label2.Text = "Did"
					Me.Label3.Text = "It, WOO-HOO PARTY TIME!!!"
					Return
				Else
					Button1.Enabled = False
				End If
				x = x + 1
			Loop
		End If

Open in new window


So in this code, consider x as your label4 value cast to int.
0
Jacques Bourgeois (James Burger)PresidentCommented:
You might have an endless loop that keeps repeating the same thing, so you do not see any change.

First, you never get out of the loop, which makes it run continuously.

Also, you have to be aware that when you are in a loop, the loop executes before anything else. Even if there are events firing in the application, they are not executed until the loop is finished. So your timer events are accumulated and will fire only when you get out of the loop. Since you never get out of the loop, they never fire.

One way to prevent that is to use an Application.DoEvents in the loop. This forces the loop to temporarily stop, processes the events, and then come back to continue looping.

I also wonder why do you need to loop for an operation that basically repeats itself. Except for the delay, nothing significant changes, unless it is done in the Timer event, which does not fire as I explained.
0
derek7467Author Commented:
Well, once a certain minute is reached, I perform another activity in the if statement.  Basically im counting down to a day in august.  Right now its just a timer, which counts down days/minutes/seconds, the reason I want a loop is because once minutes hits a certain time, at that point I execute other activities.
0
Jacques Bourgeois (James Burger)PresidentCommented:
And your timer event is never triggered because you are in a loop.

There is an easy way of checking if your loop is running or not. Simply add a Debug.Writeline("something") command after you increment x. Run the application inside of Visual Studio, trigger the call to the code, and give a look at the Output windows that shows at the bottom of the screen by default or can be activated through the Debug menu is you do not see it.

If you see a repeating "something" in the Output window, then you know that your loop is running. Try an Application.DoEvents at the end of the loop and see if it solves your problem.

An why initialize the delay with a multiplication? Why not simply give it a straight value?

What does delay(2000) does? Does your code works without it?
0
AndyAinscowFreelance programmer / ConsultantCommented:
As I mentioned earlier you are stuck in a permanent loop giving you the impression that it is not working.

>>Well, once a certain minute is reached, I perform another activity in the if statement.  Basically im counting down to a day in august.  Right now its just a timer, which counts down days/minutes/seconds, the reason I want a loop is because once minutes hits a certain time, at that point I execute other activities.

Anyway - it isn't working, you have the wrong logic.  If you want this in the timer event you do NOT require the do while loop.  (You even said yourself it did work when you didn't use the loop).
0
derek7467Author Commented:
Is there a better way to do this?

Basically when the timer hits a certain time in minutes (initiated onload), I want it to fire off a method I created.
0
Jacques Bourgeois (James Burger)PresidentCommented:
Maybe you do not understand what the Timer does. It does not pause the application, it runs a piece of code after the delay that you have specified.

- You set the Timer.Interval property to the time you want, in milliseconds, 60 000 being 1 minute.

- You start the timer.

- Once the time you have set expires, the Timer Tick event fires. Either put the code that you want to run in that event, or call your method from there.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
>>Basically when the timer hits a certain time in minutes (initiated onload), I want it to fire off a method I created.

Do you need something to display the minutes left - a countdown?
No.  Set the timer to fire after the delay you want and run the code inside the timer event.  Now stop the timer.
Yes.  Set the timer to fire every minute.  Update the countdown inside the timer event.  Check if it has reached zero.  If it has then run the extra piece of code and stop the timer.
0
derek7467Author Commented:
When I moved my code to the tick event, everything fired off correctly.

James, you were right, I didn't entirely know everything a "Timer" actually does... THANK YOU!
0
AndyAinscowFreelance programmer / ConsultantCommented:
A couple of points.
What you asked in your question is not what you wanted to know - asking what you want will often get you a better response.
Learning by doing is good, BUT use the help or a book about programming.  If you don't know something exists then you certainly will not use it.
0
derek7467Author Commented:
Andy I disagree with you. Yes my question was kinda confusing but after working with James I got exactly what I needed. I pay a price to use this site and don't just ask for answers so if you have a problem helping out when things get confusing I'd suggest you check yourself and decide whether or not you belong on this site.
0
AndyAinscowFreelance programmer / ConsultantCommented:
Confusing yes.
Have you reread your question:

I am attempting to loop an If Statement so that if a certain condition is true, it will continuously loop through the if statement until the condition is false.  When I attempt to run it with the code below, the loop doesn't happen, no errors, just doesn't happen:

Nothing about using a timer that I see there.  Also note that two experts pointed out the same error in that code you had (the loop actually was working but stopping the UI from updating), James later also pointed out the same error.


>>if you have a problem helping out when things get confusing
No problems here, my previous comment was trying to help you in your confusion.  (  I must do something.  This is something.  Therefore I must do this.   Can you spot what is wrong with that approach? )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.