I want to generate a image when a event is triggered. When the event is trigger again and the image is still generating. It will abort the generation and generate again. But my code will cause a memory leak. The application's memory usage will grow up rapidly. Does anyone know what's wrong?
edit: I put GC.Collect()
after the thread is aborted. But It's no use.
edit: The leak speed will become slower when timer's interval sets to a big number(like 2000).
Here's my code:
Public Class Form1
Dim a As Threading.Thread
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Main()
End Sub
Sub Main()
If a IsNot Nothing AndAlso a.IsAlive Then
a.Abort()
a.Join()
'GC.Collect() 'not working
'GC.WaitForPendingFinalizers() 'not working
End If
a = New Threading.Thread(AddressOf func)
a.Start()
End Sub
Sub func()
Dim newObj As Class1 = Nothing
Try
newObj = New Class1
Catch ex As Threading.ThreadAbortException
newObj?.Dispose()
End Try
End Sub
End Class
Public Class Class1
Implements IDisposable
Dim Img As New Bitmap(4095, 4095)
Public Sub Dispose() Implements IDisposable.Dispose
Img?.Dispose()
End Sub
End Class