用户界面消失,出现错误:内存不足

时间:2017-11-12 23:06:52

标签: vb.net winforms

我遇到一个问题,就是在离开程序一段时间之后让我们说5分钟或更长时间用户界面开始消失,例如:按钮,标签等等

然后,如果我尝试点击任何内容它向我显示如下错误:out of memory

我使用自定义控件:Bunifu_UI_v1.5.3

但我使用简单的东西,比如datagridview和datetimepicker

但我不认为这个问题因为我使用的几乎所有内容都是原始视觉工作室的原始控件。

有很多这样的代码:

Private Sub close_butt_MouseEnter(sender As Object, e As EventArgs) Handles close_butt.MouseEnter
    close_butt.Image = My.Resources.Close_white_32
    close_butt.BackColor = Color.Red
End Sub
Private Sub close_butt_MouseLeave(sender As Object, e As EventArgs) Handles close_butt.MouseLeave
    close_butt.Image = My.Resources.Close_white_32
    close_butt.BackColor = Nothing
End Sub

这是错误:

enter image description here

这是完整的错误文本:

System.OutOfMemoryException was unhandled
HResult=-2147024882
Message=Out of memory.
StackTrace:
   at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
   at System.Windows.Forms.ComboBox.WmReflectDrawItem(Message& m)
   at System.Windows.Forms.ComboBox.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
   at System.Windows.Forms.Control.WmOwnerDraw(Message& m)
   at System.Windows.Forms.Control.WmDrawItem(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WmSetFocus(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ComboBox.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   InnerException: 

1 个答案:

答案 0 :(得分:2)

至少有一个问题出现在这样的代码中(而且很多):

Private Sub stores_but_MouseEnter(sender As Object, e As EventArgs) Handles stores_but.MouseEnter
    stores_but.Image = My.Resources.Stores_1
    Stores_Panel.Visible = True
End Sub
Private Sub stores_but_MouseLeave(sender As Object, e As EventArgs) Handles stores_but.MouseLeave
    stores_but.Image = My.Resources.Stores
    Stores_Panel.Visible = False
End Sub

My.Resources不是包含图像或位图在内的任何内容的集合。如果您深入研究那些,您会看到如下代码:

Friend ReadOnly Property Stores() As System.Drawing.Bitmap
    Get
        Dim obj As Object = ResourceManager.GetObject("Stores", resourceCulture)
        Return CType(obj,System.Drawing.Bitmap)
    End Get
End Property

从其他地方存储的数据创建新的Bitmap对象。但是Bitmaps是一个资源,当你完成它并且输入/离开代码没有这样做时必须将其处理掉。

series of repeating images例如状态或一组是/否图像的情况下可能更糟糕:您不需要10或20个唯一图像,但这是从{{1}获取每个图像的结果}}。直接从My.Resources使用图像更新状态的计时器可能会很快耗尽句柄。

修复该问题将图片加载到数组或列表中并改为使用它们:

Myresources

在表单加载的其他地方:

Private StatusImgs As Image()

用法:

StatusImgs = New Image() {
                        My.Resources.ballblack, My.Resources.ballblue,
                        My.Resources.ballgreen,
                        My.Resources.ballorange, My.Resources.ballpurple,
                        My.Resources.ballred, My.Resources.ballyellow
                        }

现在,我的整个表单中总共会使用一个(1)绿球图像。您可以使用枚举使代码更具可读性:

myBtn.Image = StatusImgs(0)