I need to make a subroutine run continuously in VB 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • petesquawk
    New Member
    • Sep 2009
    • 15

    I need to make a subroutine run continuously in VB 2008

    I need to make a subroutine run continuously in VB 2008.

    Can I make a button permanently on? If this is possible, how can I do this? I can put all of the subroutines I want run inside the button click class.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You don't do it as "a button always on"

    Just make a loop.

    Code:
    while (true)
    {
    // true always evaluates to true, therefore the 'while' loop always keeps looping
    // Do your continual stuff here
    }
    But you might want to do something a bit more sophisticated so you can stop the loop.

    Code:
    bool IsRunning = true;
    
    // Then in your method
    while (IsRunning)
    {
    // true always evaluates to true, therefore the 'while' loop always keeps looping
    // Do your continual stuff here
    }
    
    // Later when someone hits the 'Stop' button you can
    IsRunning = false; // Causing the next time through your loop to fail and end.

    Comment

    • petesquawk
      New Member
      • Sep 2009
      • 15

      #3
      Thanks for your input.

      I tried using just a while statement and it didn't like it because the while statement wasn't in a method body.

      Code:
      Public Class Form1
          Dim isrunning As Boolean = 1
          Dim COUNTER As Long = 0
          While (isrunning)
                  RunContinuously()
          End While
      
          Private Sub RunContinuously()
              COUNTER = COUNTER + 1
              Label1.Text = COUNTER
          End Sub
      End Class


      I tried the following as well.

      Code:
      Public Class Form1
          
          Dim RUNNING As Boolean = 0
          Form1_Load()
      
          Private Sub Form1_Load()
              While (isrunning)
                  RunContinuously()
              End While
          End Sub
      
          Private Sub RunContinuously()
              COUNTER = COUNTER + 1
              Label1.Text = COUNTER
          End Sub
      End Class
      Last edited by tlhintoq; Sep 15 '09, 03:46 PM. Reason: [CODE] ...your code goes here... [/CODE] tags added

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          I tried using just a while statement and it didn't like it because the while statement wasn't in a method body
          So... Ah.... Like.... Did you have a question?

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Must have been a late night.

            Code:
            Public Class Form1
             
                Dim RUNNING As Boolean = 0
                Form1_Load()
             
                Private Sub Form1_Load()
                    While (isrunning)
                        RunContinuously()
                    End While
                End Sub
             
                Private Sub RunContinuously()
                    COUNTER = COUNTER + 1
                    Label1.Text = COUNTER
                End Sub
            End Class
            Let's see...
            • Line 3 you create a variable called "RUNNING" but in line 7 you check against "isrunning" .
            • Line 3 you initialize the variable to 0 (false) so of course when checked in line 7 the while condition fails.


            One of the easiest ways to debug this sort of thing is to put a breakpoint in at the beginning of the section you want to check (line 7 in this example) then walk through the code one line at a time (F10 key) checking that each variable has a value you are expecting. It won't take long to see behavior you weren't expecting, like code execution jumping over code you thought would run.

            Comment

            • petesquawk
              New Member
              • Sep 2009
              • 15

              #7
              Thanks for catching that wrong initialization.

              I just need to figure out how to call a subroutine at the beginning of the program. It will not accept Form1_Load().

              Public Class Form1
              Dim isrunning As Boolean = 1
              Dim COUNTER As Long = 0
              Form1_Load()

              Private Sub Form1_Load()
              While (isrunning)
              RunContinuously ()
              End While
              End Sub

              Private Sub RunContinuously ()
              COUNTER = COUNTER + 1
              Label1.Text = COUNTER
              End Sub
              End Class

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                It will not accept Form1_Load()
                Why not? Do you get an error message? I use the Form_load event all the time (C#)

                Comment

                • petesquawk
                  New Member
                  • Sep 2009
                  • 15

                  #9
                  It said "Declaratio n Expected" when I went to run the code. That was the only error.

                  Link to other question rather than confuse this thread. [tlhintoq]

                  Thank you for your help.
                  Last edited by tlhintoq; Sep 15 '09, 06:59 PM. Reason: Additional question removed as it has its own thread

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Originally posted by petesquawk
                    It said "Declaratio n Expected" when I went to run the code. That was the only error..
                    Ok... *Where* did it break when you got that error?

                    Code:
                    Public Class Form1
                     
                        Dim RUNNING As Boolean = 0
                        Form1_Load()
                     
                        Private Sub Form1_Load()
                            While (isrunning)
                                RunContinuously()
                            End While
                        End Sub
                     
                        Private Sub RunContinuously()
                            COUNTER = COUNTER + 1
                            Label1.Text = COUNTER
                        End Sub
                    End Class
                    In the properties pallet for Form1, click the Events button (yellow lightning bolt). Is this where you assigned the "Form_Load" event to your Form1_Load() handler? Did you even assign the handler to the event, or did you just type in the code for the handler thinking it would be picked up automatically and attach to the event?

                    Comment

                    Working...