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

Vb.net 用户界面消失,出现错误:内存不足,vb.net,winforms,Vb.net,Winforms,我有一个问题,在离开程序一段时间后,比如说5分钟或更长时间,用户界面开始消失,比如:按钮、标签。。。等 然后,如果我尝试单击任何内容,它会显示如下错误:内存不足 我使用的自定义控件是:Bunifu_UI_v1.5.3 但我使用简单的东西,比如datagridview和datetimepicker 但我不认为这有什么问题,因为我使用的几乎所有东西都是原始VisualStudio的原始控件 有很多这样的代码: Private Sub close_butt_MouseEnter(sender As O

我有一个问题,在离开程序一段时间后,比如说5分钟或更长时间,用户界面开始消失,比如:按钮、标签。。。等

然后,如果我尝试单击任何内容,它会显示如下错误:
内存不足

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

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

但我不认为这有什么问题,因为我使用的几乎所有东西都是原始VisualStudio的原始控件

有很多这样的代码:

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
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
这就是错误:

这是完整的错误文本:

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: 

至少有一个问题存在于这样的代码中(其中有很多问题):

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

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
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
它正在从存储在别处的数据创建一个新的位图对象。但是
位图
是一种资源,当您使用完位图后,必须将其处理掉,而Enter/Leave代码并没有这样做

如果是一个状态或一组是/否图像,情况可能会更糟:您不需要10或20个唯一的图像,但这是从
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
                        }
用法:

myBtn.Image = StatusImgs(0)
现在,在我的整个表单中将总共使用一个绿色的球图像。可以使用枚举使代码更具可读性:

Private Enum Status
    Away 
    Busy 
    IgnoringYou
    Dead
    ...
End Enum

myBtn.Image = StatusImgs(Status.Busy)

你的帖子让人困惑:它都是标准控件还是有自定义控件?您是否在任何地方进行任何特殊的绘图/绘画(
shops\u diagram
让这看起来很有可能)?一般来说,这听起来像是一个资源不足的问题(没有更多可用于控制的句柄),这通常意味着泄漏,可能是因为没有处理东西。这些都是应用程序范围内的弊病,如果代码是这样的话,就不会有结果。顺便说一句,Visual Studio提供了一些很棒的分析工具,可以向您显示泄漏的大致位置。@Protoix我使用的是标准控件,除了
datagridview
datetimepicker
,我不知道它的表单是否没有处理,因为当我运行程序并且不输入任何内容或执行任何操作就离开它时,问题也会发生。你能给我一个这个工具或链接的名字吗。谢谢。您是否在任何地方进行任何特殊的绘图/绘画(
shops\u diagram
使之看起来很可能)?如果它什么也不做,那就是你在某处做的事情。请注意,表单是应该处理的资源,而显示的代码则不是(看起来可能是默认的表单引用)。所有的分析工具都在VS中。我的在分析菜单下,但可能因版本而异。这篇文章中的相关信息太少,除了一般的指导方针之外,没有太多帮助。@Protoix不,我没有使用任何需要画画的东西。这是我的主要表格的完整代码,如果你能看一下,我会很感激的。谢谢你让我度过了美好的一天,你是我的朋友,我非常感激,真的谢谢你。