Foros del Web » Programación para mayores de 30 ;) » .NET »

Timer "acaparaRAM"

Estas en el tema de Timer "acaparaRAM" en el foro de .NET en Foros del Web. Estoy en medio de un prorgamita y en el tengo un timer que es tal que asi Código: Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal ...
  #1 (permalink)  
Antiguo 02/02/2012, 16:26
Avatar de juanito1712  
Fecha de Ingreso: mayo-2010
Ubicación: Valencia
Mensajes: 1.124
Antigüedad: 14 años
Puntos: 66
Timer "acaparaRAM"

Estoy en medio de un prorgamita y en el tengo un timer que es tal que asi


Código:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If IsWindowVisible(GetForegroundWindow()) And statuswindow = False Then
            Dim rct As RECT
            Dim victima As Long = GetForegroundWindow()
            GetWindowRect(GetForegroundWindow(), rct)

            Dim bmpScreenshot As Bitmap = New Bitmap(rct.Right - rct.Left, rct.Bottom - rct.Top, PixelFormat.Format24bppRgb)
            Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
            gfxScreenshot.CopyFromScreen(rct.Left, rct.Top, 0, 0, New Size(rct.Right - rct.Left, rct.Bottom - rct.Top), CopyPixelOperation.SourceCopy)


            Try
                bmpScreenshot.Save(sTempFolderPath & victima & ".png")
            Catch ex As Exception

            End Try



        End If

    End Sub
y el tio no hace mas que acaparar mas y mas ram a cada ciclo que es ejecutado y cuando el asunto empieza a ser mosquenate el solito se vacia y vuelve a ir subiendo

veis algo que pueda hacer para que no esté ahi subiendo y bajando la ram?

Última edición por juanito1712; 02/02/2012 a las 16:50
  #2 (permalink)  
Antiguo 02/02/2012, 17:25
 
Fecha de Ingreso: abril-2011
Mensajes: 1.342
Antigüedad: 13 años
Puntos: 344
Respuesta: Timer "acaparaRAM"

Creo que el problema es que no liberas los recursos del objeto graphics ni del objeto Bitmap. La forma de hacer esto es llamar al método Dispose al terminar con ellos o usar using.

Con Using:

Código VB:
Ver original
  1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         If IsWindowVisible(GetForegroundWindow()) And statuswindow = False Then
  3.             Dim rct As RECT
  4.             Dim victima As Long = GetForegroundWindow()
  5.             GetWindowRect(GetForegroundWindow(), rct)
  6.  
  7.            Using bmpScreenshot As New Bitmap(rct.Right - rct.Left, rct.Bottom - rct.Top, PixelFormat.Format24bppRgb)
  8. Using gfxScreenshot As Graphics.FromImage(bmpScreenshot)
  9.             gfxScreenshot.CopyFromScreen(rct.Left, rct.Top, 0, 0, New Size(rct.Right - rct.Left, rct.Bottom - rct.Top), CopyPixelOperation.SourceCopy)
  10.  
  11.  
  12.             Try
  13.                 bmpScreenshot.Save(sTempFolderPath & victima & ".png")
  14.             Catch ex As Exception
  15.  
  16.             End Try
  17.  
  18.            End Using
  19. End Using
  20.         End If
  21.  
  22.     End Sub


Sin Using:

Código VB:
Ver original
  1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         If IsWindowVisible(GetForegroundWindow()) And statuswindow = False Then
  3.             Dim rct As RECT
  4.             Dim victima As Long = GetForegroundWindow()
  5.             GetWindowRect(GetForegroundWindow(), rct)
  6.  
  7.             Dim bmpScreenshot As Bitmap = New Bitmap(rct.Right - rct.Left, rct.Bottom - rct.Top, PixelFormat.Format24bppRgb)
  8.             Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
  9.             gfxScreenshot.CopyFromScreen(rct.Left, rct.Top, 0, 0, New Size(rct.Right - rct.Left, rct.Bottom - rct.Top), CopyPixelOperation.SourceCopy)
  10.  
  11.  
  12.             Try
  13.                 bmpScreenshot.Save(sTempFolderPath & victima & ".png")
  14.             Catch ex As Exception
  15.  
  16.             End Try
  17.  
  18.             gfxScreenshot.Dispose();
  19. bmpScreenshot.Dispose();
  20.  
  21.  
  22.         End If
  23.  
  24.     End Sub
  #3 (permalink)  
Antiguo 02/02/2012, 18:01
Avatar de juanito1712  
Fecha de Ingreso: mayo-2010
Ubicación: Valencia
Mensajes: 1.124
Antigüedad: 14 años
Puntos: 66
Respuesta: Timer "acaparaRAM"

perfecto
muchas gracias!!!

Etiquetas: time
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:44.