Results 1 to 12 of 12

Thread: [RESOLVED][2008]Infinite Loop (I need one)...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Resolved [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
    Last edited by goldenix; Apr 29th, 2008 at 03:32 AM.

    M.V.B. 2008 Express Edition

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    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

    M.V.B. 2008 Express Edition

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    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

    M.V.B. 2008 Express Edition

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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?
    My usual boring signature: Nothing

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Infinite Loop (I need one)...

    Maybe you should explain what exactly you are trying to do and why?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    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.

    M.V.B. 2008 Express Edition

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED][2008]Infinite Loop (I need one)...

    Could you not just use the TextChanged event of the TextBox to do this?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width