显示屏关闭时,图片框内存溢出

时间:2014-03-05 01:57:44

标签: vb.net out-of-memory picturebox

我创建了一个VB.NET表单应用程序,可以在每个tick事件中将新图片加载到图片框中。但是,当我的计算机显示屏关闭时,exe的内存使用量会慢慢上升。如果我在短时间内移动鼠标并且屏幕亮起,则内存会突然恢复正常。但是,如果我关闭屏幕,内存使用量将继续攀升并最终导致内存不足异常。此外,图片框图像变成了一个大红色的X.

如何检测屏幕关闭? - 完成

如何从msg中获取POWERBROADCAST_SETTING结构(lParam)数据?
http://msdn.microsoft.com/en-us/library/aa373247.aspx
http://msdn.microsoft.com/en-us/library/aa372723.aspx

Declare Function RegisterPowerSettingNotification Lib "user32" (ByVal hRecipient As IntPtr, ByRef PowerSettingGuid As Guid, ByVal Flags As Int32) As IntPtr

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim GG As System.Guid
    System.Guid.TryParse("02731015-4510-4526-99e6-e5a17ebd1aea", GG)
    RegisterPowerSettingNotification(Me.Handle, GG, 0)
    '...
End Sub
Protected Overrides Sub WndProc(ByRef msg As System.Windows.Forms.Message)
    'MessageBox.Show(msg.Msg)
    If msg.WParam = &H8013 Then
        Console.WriteLine(msg.ToString)
    End If
    MyBase.WndProc(msg)
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'code to load some image using PictureBox1.Load()
End Sub

控制台输出:
msg = 0x218(WM_POWERBROADCAST)hwnd = 0x9108a wparam = 0x8013 lparam = 0x57f5080 result = 0x0
msg = 0x218(WM_POWERBROADCAST)hwnd = 0x9108a wparam = 0x8013 lparam = 0x57f50e0 result = 0x0
msg = 0x218(WM_POWERBROADCAST)hwnd = 0x9108a wparam = 0x8013 lparam = 0x57f4d20 result = 0x0
msg = 0x218(WM_POWERBROADCAST)hwnd = 0x9108a wparam = 0x8013 lparam = 0x57f5020 result = 0x0
msg = 0x218(WM_POWERBROADCAST)hwnd = 0x9108a wparam = 0x8013 lparam = 0x57f4d60 result = 0x0
msg = 0x218(WM_POWERBROADCAST)hwnd = 0x9108a wparam = 0x8013 lparam = 0x57f5180 result = 0x0

抱歉格式化...刚加入此网站。

修改 这似乎有效。

Public Structure PB_S
    Public PowerSetting As Guid
    Public DataLength As UInteger  'dword (32-bit unsigned int)
    Public Data As Byte 'uchar
End Structure
Dim P_inst As PB_S

P_inst = CType(Marshal.PtrToStructure(msg.LParam, GetType(PB_S)), PB_S)

控制台打印出P_inst中的内容:

msg = 0x218(WM_POWERBROADCAST)hwnd = 0x14a10fe wparam = 0x8013 lparam = 0x295048 result = 0x0
0
4
02731015-4510-4526-99e6-e5a17ebd1aea

msg = 0x218(WM_POWERBROADCAST)hwnd = 0x14a10fe wparam = 0x8013 lparam = 0x295048 result = 0x0
1
4
02731015-4510-4526-99e6-e5a17ebd1aea

1 个答案:

答案 0 :(得分:0)

要检测计算机是否已锁定,屏幕保护程序或睡眠,请参阅此处的帖子:http://www.vbforums.com/showthread.php?228575-resolved-Detect-Screen-Saver-Sleep-Mode-Computer-locked&p=1348493&viewfull=1#post1348493。代码适用于VB6,但它也适用于VB.Net(稍作修改)。如果它不起作用,只需评论这篇文章。