[RESOLVED][2008]Infinite Loop (I need one)...
I dont get it now, all Infinite Loop codes I tried, are crashing my app.
question: is there a way to do this? Any other way except using a timer ?
so basically I dont want to press buttons, instead ill let it run in a loop & if i enter new value it will be calculated automatically
Code:
While 1
System.Threading.Thread.Sleep(200)
Dim var As Integer = TextBox1.Text * 2
TextBox2.Text = var
End While
EDIT: Solution: Use timers, you cant infinite loop in V.b
Re: Infinite Loop (I need one)...
An infinite loop will always end in crashing because the system will run out of memory.
An infinite loop is considered a software bug in most practical terms.. So if you are trying to accomplish something specific, you are likely just going about it the wrong way.
Re: Infinite Loop (I need one)...
I'm guessing that your app looks like its crashing because the GUI cannot be updated due to your program being stuck in this infinite loop.
You need to use another thread (BackgroundWorker Control) to perform your looping so that the GUI still responds.
Or did you mean its throwing an error?
Re: Infinite Loop (I need one)...
Quote:
Originally Posted by chris128
I'm guessing that your app looks like its crashing because the GUI cannot be updated due to your program being stuck in this infinite loop.
As you said, its stuck & gui is not responding. Can you possibly make an example with that back...ground thing? I have those 2 functions, but I have no idea, how to start or how to stop looping.
Code:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
End Sub
Re: Infinite Loop (I need one)...
would it be easier to just put the calculation code in a timer tick event? That way you wouldn't need to use sleep or while loops. Although with 200 milliseconds it's going to be a bit strange.
Re: Infinite Loop (I need one)...
An Application.DoEvents in the loop would make the app responsive, but without any way to end the loop, or any precautions, that calculation will crash after about 32 cycles when the integer overflows.
Re: Infinite Loop (I need one)...
Quote:
Originally Posted by Shaggy Hiker
An Application.DoEvents in the loop would make the app responsive, but without any way to end the loop, or any precautions, that calculation will crash after about 32 cycles when the integer overflows.
So Why not restart the whole thing after 1 sycle?
Like this? but looks like it isnt working
Code:
TestDoEvents()
Private Sub TestDoEvents()
System.Threading.Thread.Sleep(200)
Dim var As Integer = TextBox1.Text * 2
TextBox2.Text = var
My.Application.DoEvents()
End Sub
Re: Infinite Loop (I need one)...
Well, you are calling the sub from nowhere, so that exact code won't work, unless TestDoEvents is inside a sub. However, this is getting weirder all the time. Infinite loops are generally only used in games, and there is always a way to get out of them.
By the way, what's wrong with using a timer?
Re: Infinite Loop (I need one)...
Maybe you should explain what exactly you are trying to do and why?
Re: Infinite Loop (I need one)...
Quote:
Originally Posted by chris128
Maybe you should explain what exactly you are trying to do and why?
nahh this all requires just too much code. Guess Ill stick with the timers. They seem to be the most practical solution.
& as i said earlier I have 2 textboxes 1 & 2 . so user will enter numeric value into box 1 & then that value will be multiplied by 2 automatically & the answer splitted by coma & the lehght of the second split substracted to 2 chars like this (10,15 for example.) so user wont have to waist his time on pressing any buttons.
Re: [RESOLVED][2008]Infinite Loop (I need one)...
Could you not just use the TextChanged event of the TextBox to do this?
Re: [RESOLVED][2008]Infinite Loop (I need one)...
Quote:
Originally Posted by chris128
Could you not just use the TextChanged event of the TextBox to do this?
Exactly.... Handle the appropriate event of the control instead of using an infinite loop. In your case, any one of these events will do the job: TextChanged, KeyPress, KeyDown, KeyUp.