J
JohnnyGr
Im trying to figure out how i can cancel the currently running thread when
the lwFiles_ItemSelectionChanged sub is triggered... the purpose of the
threading
is to load a thumbnail for a image, these images can be pretty large but i
still
want to be able to change image in my file list and start loading a new
image instead
this works now but not that good really, since the old thread is running in
the background
until it hits the If Not CurrentImage = filename Then... clause...
does anyone have any idea on how i can kill the old thread before invoking
the new one?
Im using framework 2.0
I have the following code so far...
Private ad As New GetImageDataDelegate(AddressOf
GetImageExifAndThumbnail)
Delegate Sub GetImageDataDelegate(ByVal filename As String)
Delegate Sub UpdateThumbnailAndExifDelegate(ByRef thumbnail As Image,
ByRef exifdata As Hashtable)
Dim CurrentImage As String
'<Summary>
' Triggers when a image is selected in the file list
' and gets the exif information and thumbnail
'</Summary>
Private Sub lwFiles_ItemSelectionChanged(ByVal sender As Object, ByVal e
As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles
lwFiles.ItemSelectionChanged
If e.IsSelected Then
Try
Dim CallBack As New AsyncCallback(AddressOf
GetImageExifAndThumbnailCallBack)
Dim ia As IAsyncResult = ad.BeginInvoke(e.Item.Tag,
CallBack, ad)
Catch ex As Exception
End Try
End If
End Sub
Private Sub UpdateThumbnailAndExif(ByRef thumbnail As Image, ByRef
exifdata As Hashtable)
Debug.WriteLine(thumbnail.Width)
If Me.picPreview.InvokeRequired Then
Dim d As New UpdateThumbnailAndExifDelegate(AddressOf
UpdateThumbnailAndExif)
Me.Invoke(d, New Object() {thumbnail, exifdata})
Else
Me.picPreview.Image = thumbnail
End If
End Sub
Private Sub GetImageExifAndThumbnail(ByVal filename As String)
CurrentImage = filename
Debug.WriteLine(filename)
Dim hash As Hashtable = Nothing
Dim image As New Bitmap(filename, True)
If Not CurrentImage = filename Then
image.Dispose()
Debug.Write("Exiting thread")
Exit Sub
End If
Dim thumbnail As Image
thumbnail = image.GetThumbnailImage(100, 100, Nothing, Nothing)
image.Dispose()
If CurrentImage = filename Then
Dim uidel As New UpdateThumbnailAndExifDelegate(AddressOf
UpdateThumbnailAndExif)
uidel.Invoke(thumbnail, hash)
Else
image.Dispose()
hash.Clear()
End If
End Sub
Private Sub GetImageExifAndThumbnailCallBack(ByVal ia As IAsyncResult)
Debug.WriteLine("callback")
CType(ia.AsyncState, GetImageDataDelegate).EndInvoke(ia)
End Sub
the lwFiles_ItemSelectionChanged sub is triggered... the purpose of the
threading
is to load a thumbnail for a image, these images can be pretty large but i
still
want to be able to change image in my file list and start loading a new
image instead
this works now but not that good really, since the old thread is running in
the background
until it hits the If Not CurrentImage = filename Then... clause...
does anyone have any idea on how i can kill the old thread before invoking
the new one?
Im using framework 2.0
I have the following code so far...
Private ad As New GetImageDataDelegate(AddressOf
GetImageExifAndThumbnail)
Delegate Sub GetImageDataDelegate(ByVal filename As String)
Delegate Sub UpdateThumbnailAndExifDelegate(ByRef thumbnail As Image,
ByRef exifdata As Hashtable)
Dim CurrentImage As String
'<Summary>
' Triggers when a image is selected in the file list
' and gets the exif information and thumbnail
'</Summary>
Private Sub lwFiles_ItemSelectionChanged(ByVal sender As Object, ByVal e
As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles
lwFiles.ItemSelectionChanged
If e.IsSelected Then
Try
Dim CallBack As New AsyncCallback(AddressOf
GetImageExifAndThumbnailCallBack)
Dim ia As IAsyncResult = ad.BeginInvoke(e.Item.Tag,
CallBack, ad)
Catch ex As Exception
End Try
End If
End Sub
Private Sub UpdateThumbnailAndExif(ByRef thumbnail As Image, ByRef
exifdata As Hashtable)
Debug.WriteLine(thumbnail.Width)
If Me.picPreview.InvokeRequired Then
Dim d As New UpdateThumbnailAndExifDelegate(AddressOf
UpdateThumbnailAndExif)
Me.Invoke(d, New Object() {thumbnail, exifdata})
Else
Me.picPreview.Image = thumbnail
End If
End Sub
Private Sub GetImageExifAndThumbnail(ByVal filename As String)
CurrentImage = filename
Debug.WriteLine(filename)
Dim hash As Hashtable = Nothing
Dim image As New Bitmap(filename, True)
If Not CurrentImage = filename Then
image.Dispose()
Debug.Write("Exiting thread")
Exit Sub
End If
Dim thumbnail As Image
thumbnail = image.GetThumbnailImage(100, 100, Nothing, Nothing)
image.Dispose()
If CurrentImage = filename Then
Dim uidel As New UpdateThumbnailAndExifDelegate(AddressOf
UpdateThumbnailAndExif)
uidel.Invoke(thumbnail, hash)
Else
image.Dispose()
hash.Clear()
End If
End Sub
Private Sub GetImageExifAndThumbnailCallBack(ByVal ia As IAsyncResult)
Debug.WriteLine("callback")
CType(ia.AsyncState, GetImageDataDelegate).EndInvoke(ia)
End Sub