System.StackOverflowException

Overflow from Me.Refresh

Page 1 of 1

3 Replies - 3254 Views - Last Post: 21 December 2008 - 09:06 PM Rate Topic: -----

#1 melvinsmiley132  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-December 08

System.StackOverflowException

Posted 21 December 2008 - 06:48 PM

I am getting a stack overflow after about 5 seconds of running the following code. It is indeed doing a refresh every 33 ms but that shouldn't be causing an overflow should it? The code for the project is below, it's basically a really horrible looking sine wave animation (just doing some background with the new GDI+ drawing tools before I start a game).


Public Class Form1
	Public Running As Boolean
	Public myGraphics As Graphics
	Public myPen As Pen
	Public myPoints(3) As Point
	Public UpDown1 As Integer
	Public UpDown2 As Integer

	Private Sub DrawNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawNow.Click
		InitializeLand()
	End Sub

	Public Sub InitializeLand()

		myPoints(0) = New Point(0, Me.Height / 2)
		myPoints(1) = New Point(Me.Width * 0.25, Me.Height / 2)
		myPoints(2) = New Point(Me.Width * 0.75, Me.Height / 2)
		myPoints(3) = New Point(Me.Width, Me.Height / 2)

		UpDown1 = 1
		UpDown2 = -1
		Running = True
	End Sub

	Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
		If Running = True Then
			myPoints(0).Y = myPoints(0).Y + (UpDown2 * (Me.Height / 500))
			myPoints(1).Y = myPoints(1).Y + (UpDown1 * (Me.Height / 500))
			myPoints(2).Y = myPoints(2).Y + (UpDown2 * (Me.Height / 500))
			myPoints(3).Y = myPoints(3).Y + (UpDown1 * (Me.Height / 500))

			If myPoints(1).Y >= Me.Height Or myPoints(2).Y >= Me.Height Then
				UpDown1 = -UpDown1
				UpDown2 = -UpDown2
			End If

			e.Graphics.Clear(Color.LightGray)
			myPen = New Pen(Color.Blue, Width:=4)

			e.Graphics.DrawCurve(myPen, myPoints, 0.67)
		End If
	End Sub

	Private Sub tmrSine_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSine.Tick
		Do While Running = True
			Me.Refresh()
			Application.DoEvents()
		Loop
	End Sub
End Class



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: System.StackOverflowException

#2 melvinsmiley132  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-December 08

Re: System.StackOverflowException

Posted 21 December 2008 - 06:53 PM

Doh I dunno why I put a Do While loop in the timer... that pretty much explains that lol, sorry guys

This post has been edited by melvinsmiley132: 21 December 2008 - 06:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: System.StackOverflowException

Posted 21 December 2008 - 06:58 PM

Moved to VB.NET :)
Was This Post Helpful? 1

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: System.StackOverflowException

Posted 21 December 2008 - 09:06 PM

Exception is from you not properly disposing of the pens.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1