What could be causing my destinationVariable to not update in my timer program?

In summary, The program is not producing any results from the "destinationVariable" after being debugged and run. The function "myfunction" is supposed to run in a loop, but there may be an issue with the "if" statement not being able to evaluate to true due to the quick execution of the lines of code.
  • #1
angelspikes
10
0
I've debugged and run my program without seeing any results from my "destinationVariable".
What could be wrong?

sub main()

call myfunction()

end sub

sub myfunction()

static myClock As Single

myClock = timer

if (timer - myClock) > 100 Then
destinationVariable += 1 // This variable is linked to this script
End If

End Sub

// I want the function to run in a loop, but I guess I don't need to use the do loop function to make that happen?
 
Technology news on Phys.org
  • #2
angelspikes said:
myClock = timer
if (timer - myClock) > 100 Then

Since these lines execute right after each other, I doubt that the "if" test will ever be true unless the program gets interrupted between the two statements. That would be "rare". It may happen occasionally if you put this in a hard loop and run it continuously, but the odds on anyone run are very small.
 

1. How can I create a timer program in Visual Basic?

To create a timer program in Visual Basic, you can use the Timer control in the Toolbox. Simply drag and drop the Timer control onto your form and then use the properties window to set the interval (in milliseconds) for the timer.

2. How do I start and stop the timer in my program?

You can start and stop the timer by using the Start and Stop methods of the Timer control. These methods can be called from a button click event or any other event in your program.

3. Can I change the interval of the timer during runtime?

Yes, you can change the interval of the timer during runtime by using the Interval property of the Timer control. You can set the interval to a new value and then call the Start method to restart the timer with the new interval.

4. How can I perform an action every time the timer ticks?

You can use the Tick event of the Timer control to perform an action every time the timer ticks. You can add your code in the event handler for the Tick event and it will be executed each time the timer reaches its set interval.

5. Is there a way to make the timer stop after a certain number of ticks?

Yes, you can use a counter variable to keep track of the number of ticks and then use an if statement to stop the timer once the desired number of ticks is reached. You can also use the Enabled property of the Timer control to control when the timer starts and stops.

Similar threads

  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
820
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
1
Views
947
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top