【问题标题】:Cannot access a disposed object on form close无法在表单关闭时访问已处置的对象
【发布时间】:2017-12-12 09:36:03
【问题描述】:

我正在尝试为具有网格和刻度计数器的游戏编写一些代码,但网格只是尝试填充窗口而不是停止并且不显示刻度计数器。不断出现的错误是:

System.Windows.Forms.dll 中发生了“System.ObjectDisposedException”类型的第一次机会异常

我不知道这意味着什么,也不知道如何解决。

这是我的代码:

Public Class Form1
    Dim G As Graphics
    Dim BBG As Graphics
    Dim BB As Bitmap
    Dim r As Rectangle

    Dim tSec As Integer = TimeOfDay.Second
    Dim tTicks As Integer = 0
    Dim MaxTicks As Integer = 0

    Dim IsRunning As Boolean = True

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        Me.Focus()

        G = Me.CreateGraphics
        BB = New Bitmap(Me.Width, Me.Height)

        StartGameLoop()
    End Sub

    Private Sub DrawGraphics()
        For X = 0 To 19
            For Y = 0 To 14
                r = New Rectangle(X * 32, Y * 32, 32, 32)

                G.FillRectangle(Brushes.BurlyWood, r)
                G.DrawRectangle(Pens.Black, r)
            Next
        Next
        G.DrawString("Ticks: " & tTicks & vbCrLf & _
                    "TPS: " & MaxTicks, Me.Font, Brushes.Black, 650, 0)

        G = Graphics.FromImage(BB)

        BBG = Me.CreateGraphics
        BBG.DrawImage(BB, 0, 0, Me.Width, Me.Height)

        G.Clear(Color.Wheat)
    End Sub

    Private Sub StartGameLoop()
        Do While IsRunning = True
            Application.DoEvents()



            DrawGraphics()

            TickCounter()
        Loop
    End Sub

    Private Sub TickCounter()
        If tSec = TimeOfDay.Second And IsRunning = True Then
            tTicks = tTicks + 1
        Else
            MaxTicks = tTicks
            tTicks = 0
            tSec = TimeOfDay.Second
        End If
    End Sub
End Class

【问题讨论】:

  • 你可以试试我给你的技巧,你可能会帮助你找到你的问题

标签: vb.net exception exception-handling


【解决方案1】:

你在这里使用了很多不好的做法......

首先,使用Me.CreateGraphics()是不好的,结果对象只能绘制一次,这意味着你不得不多次调用它。不断调用它只会创建越来越多的图形对象,从而增加内存使用量。即使您每次完成绘图时都要处理它们,但它仍然是一个巨大的瓶颈,因为它会减慢处理速度。

其次,使用Application.DoEvents()非常糟糕的做法,并且会像这样循环烧毁你的 CPU。除非正确使用(您没有正确使用),否则它可能会导致意外和不可预测的行为。您遇到的错误就是这种意外行为的一个很好的例子。

我建议你阅读这个 MSDN 博客,它准确地解释了为什么一个不应该使用Application.DoEvents():Keeping your UI Responsive and the Dangers of Application.DoEvents


相反,为了正确执行此操作:

  • Me.CreateGraphics() 替换为表单的Paint event 并通过e.Graphics 对象在其中完成所有绘图。

  • 将您的游戏循环替换为Timer,该Timer 不断调用Me.Invalidate() 以重绘表单。

例如:

Dim WithEvents GameTimer As New Timer() With {.Interval = 1}

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    GameTimer.Start()
End Sub

Private GameTimer_Tick(sender As System.Object, e As System.EventArgs) Handles GameTimer.Tick
    Me.Invalidate() 'Redraw the form.
    TickCounter()
End Sub

Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    e.Graphics.Clear(Color.Wheat)

    For X = 0 To 19
        For Y = 0 To 14
            Dim r As New Rectangle(X * 32, Y * 32, 32, 32)

            e.Graphics.FillRectangle(Brushes.BurlyWood, r)
            e.Graphics.DrawRectangle(Pens.Black, r)
        Next
    Next

    e.Graphics.DrawString("Ticks: " & tTicks & vbCrLf & _
                          "TPS: " & MaxTicks, Me.Font, Brushes.Black, 650, 0)
End Sub

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多