我有一个问题,即离开程序一段时间后,假设 5 分钟或更长时间,用户界面开始消失,例如:按钮、标签...等
然后,如果我尝试单击任何内容,它会显示如下错误:内存不足
我使用的自定义控件是:Bunifu_UI_v1.5.3
但我使用简单的东西,如 datagridview 和 datetimepicker
但我不认为这有问题,因为我使用的几乎所有东西都是原始 Visual Studio 的原始控件。
有很多这样的代码:
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
这是错误:
这是完整的错误文本:
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:
最佳答案
至少有一个问题出现在这样的代码中(而且很多):
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 对象。但是位图是一种资源,在使用完毕后必须将其释放,而 Enter/Leave 代码不会执行此操作。
在 series of repeating images 的情况下情况可能会更糟例如状态或一组是/否图像:您不需要 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)
现在,我的整个表单中将总共使用一 (1) 个绿球图像。您可以使用枚举使代码更具可读性:
Private Enum Status
Away
Busy
IgnoringYou
Dead
...
End Enum
myBtn.Image = StatusImgs(Status.Busy)
关于vb.net - 用户界面消失并出现错误 : out of memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47255019/