Reputation: 851
I have a program that is throwing a StackOverflowException when my code is run. I don't quite know what's causing it, but I think is has something to do with the fact that the code is run about 100 times a second.
I have a timer (Timer1) that has an interval of 1 millisecond. I want the code to run as fast as possible without using a do...loop.
Here's my code. Before anyone asks, yes this was designed to slow a computer down.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Not NumericUpDown2.Value >= NumericUpDown1.Value Then
NumericUpDown2.Value += 1
Do Until CheckBox1.Checked
Application.DoEvents()
Loop
NumericUpDown2.Value -= 1
Else
Timer1.Enabled = False
End If
End Sub
Is runs fine until NumericUpDown2.Value
is around 800
-1000
, then it throws the error when NumericUpDown2.Value += 1
is run.
The maximum for NumericUpDown1
and NumericUpDown2
is 10000
.
Upvotes: 0
Views: 403
Reputation: 338
It looks to me like a code reentry problem. Your code is looping inside this event, when you call do events. This allows the app to update, and process events, which includes your loop, and so on.
So your code ultimately runs as
Do
Doevents
Do
Doevents
Do
Doevents
Do
Doevents
.....
And so on, each time the stack is growing to maintain the program counter within each of the loops, and eventually causes an overflow.
Upvotes: 1
Reputation:
The problem is likely that you are calling DoEvents() from a timer. The doevents likely picks up a timer message, which calls DoEvents(). Etc etc..
Upvotes: 2