Connecting Tech Pros Worldwide Forums | Help | Site Map

I need to make a subroutine run continuously in VB 2008

Newbie
 
Join Date: Sep 2009
Posts: 15
#1: Sep 9 '09
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's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#2: Sep 9 '09

re: I need to make a subroutine run continuously in VB 2008


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

Just make a loop.

Expand|Select|Wrap|Line Numbers
  1. while (true)
  2. {
  3. // true always evaluates to true, therefore the 'while' loop always keeps looping
  4. // Do your continual stuff here
  5. }
But you might want to do something a bit more sophisticated so you can stop the loop.

Expand|Select|Wrap|Line Numbers
  1. bool IsRunning = true;
  2.  
  3. // Then in your method
  4. while (IsRunning)
  5. {
  6. // true always evaluates to true, therefore the 'while' loop always keeps looping
  7. // Do your continual stuff here
  8. }
  9.  
  10. // Later when someone hits the 'Stop' button you can
  11. IsRunning = false; // Causing the next time through your loop to fail and end.
  12.  
  13.  
Newbie
 
Join Date: Sep 2009
Posts: 15
#3: Sep 15 '09

re: I need to make a subroutine run continuously in VB 2008


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.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim isrunning As Boolean = 1
  3.     Dim COUNTER As Long = 0
  4.     While (isrunning)
  5.             RunContinuously()
  6.     End While
  7.  
  8.     Private Sub RunContinuously()
  9.         COUNTER = COUNTER + 1
  10.         Label1.Text = COUNTER
  11.     End Sub
  12. End Class


I tried the following as well.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Dim RUNNING As Boolean = 0
  4.     Form1_Load()
  5.  
  6.     Private Sub Form1_Load()
  7.         While (isrunning)
  8.             RunContinuously()
  9.         End While
  10.     End Sub
  11.  
  12.     Private Sub RunContinuously()
  13.         COUNTER = COUNTER + 1
  14.         Label1.Text = COUNTER
  15.     End Sub
  16. End Class
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#4: Sep 15 '09

re: I need to make a subroutine run continuously in VB 2008


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.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#5: Sep 15 '09

re: I need to make a subroutine run continuously in VB 2008


Quote:
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?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#6: Sep 15 '09

re: I need to make a subroutine run continuously in VB 2008


Quote:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Dim RUNNING As Boolean = 0
  4.     Form1_Load()
  5.  
  6.     Private Sub Form1_Load()
  7.         While (isrunning)
  8.             RunContinuously()
  9.         End While
  10.     End Sub
  11.  
  12.     Private Sub RunContinuously()
  13.         COUNTER = COUNTER + 1
  14.         Label1.Text = COUNTER
  15.     End Sub
  16. 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.
Newbie
 
Join Date: Sep 2009
Posts: 15
#7: Sep 15 '09

re: I need to make a subroutine run continuously in VB 2008


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().

Quote:
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
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#8: Sep 15 '09

re: I need to make a subroutine run continuously in VB 2008


Quote:
It will not accept Form1_Load()
Why not? Do you get an error message? I use the Form_load event all the time (C#)
Newbie
 
Join Date: Sep 2009
Posts: 15
#9: Sep 15 '09

re: I need to make a subroutine run continuously in VB 2008


It said "Declaration 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.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#10: Sep 15 '09

re: I need to make a subroutine run continuously in VB 2008


Quote:

Originally Posted by petesquawk View Post

It said "Declaration Expected" when I went to run the code. That was the only error..

Ok... *Where* did it break when you got that error?

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Dim RUNNING As Boolean = 0
  4.     Form1_Load()
  5.  
  6.     Private Sub Form1_Load()
  7.         While (isrunning)
  8.             RunContinuously()
  9.         End While
  10.     End Sub
  11.  
  12.     Private Sub RunContinuously()
  13.         COUNTER = COUNTER + 1
  14.         Label1.Text = COUNTER
  15.     End Sub
  16. 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?
Reply

Tags
button permanently, continously, subroutine