Major image list problem!

  • Thread starter Thread starter Nak
  • Start date Start date
N

Nak

Hi there,

I've come across quite an annoying bug after optimizing some of my code
(which sounds quite strange I know), but take this for a description of my
problem.

* I have a background thread continually monitoring a collection, when
items are available in the collection they are dealt with.
* The items in question are files for images, when files appear in the
collection the thread makes a thumbnail for it (which is associated with a
listview control) and then sets it to the relevant list view item.

Basically this method is used to enable me to process thumbnails in my
thumbnail browser *only* when they are visible in the client area. Anyway,
my problem comes *after* adding an image, consider the following code

------------------------------------------

Private Sub updateImageThumbnail(ByVal iItem As ListViewItem, ByVal
iThumbnail As Image)

SyncLock ilsThumbnails.Images
^This had no effect in resolving the bug

Dim pIntIndex As Integer = ilsThumbnails.Images.Count

Console.WriteLine("thumbnails = " &
ilsThumbnails.Images.Count.ToString)

Call ilsThumbnails.Images.Add(iThumbnail)

Console.WriteLine("after add thumbnails = " &
ilsThumbnails.Images.Count.ToString)

Call ilsSmall.Images.Add(smallImageIcon)
^This line has no relevance in this snipette

With iItem

Console.WriteLine("setting image index to " &
pIntIndex.ToString)

.ImageIndex = pIntIndex
^ The exception occurs here, I have copied and pasted the output
from the console below this code snipette

Console.WriteLine("image index set to " & pIntIndex.ToString)

End With

End SyncLock

End Sub

--------------------------------------------------------------------

thumbnails = 2
after add thumbnails = 3
setting image index to 2
The thread 'xObjects.thumbnailBrowser.pTrdUpdateThread' (0x1ac) has exited
with code 0 (0x0).
The thread 'xObjects.thumbnailBrowser.pTrdDrawThread' (0xc04) has exited
with code 0 (0x0).
System.ArgumentException: '-1' is not a valid value for 'index'.
at System.Windows.Forms.ListView.SetItemImage(Int32 index, Int32 image)
at System.Windows.Forms.ListViewItem.set_ImageIndex(Int32 value)
at NickPateman.xObjects.thumbnailBrowser.updateImageThumbnail(ListViewItem
iItem, Image iThumbnail) in E:\Personal\Development\VB
dotnet\NickPateman\xObjects\User controls\thumbnailBrowser.vb:line 373
at NickPateman.xObjects.thumbnailBrowser.pTrdDrawThread_Callback() in
E:\Personal\Development\VB dotnet\NickPateman\xObjects\User
controls\thumbnailBrowser.vb:line 648

--------------------------------------------------------------------

Any ideas as to what the hell is happening because I haven't got the
faintest idea?!?! All I can imagine is that the listview is still doing
something in the main thread. This problem is intermittent at the best of
times. I added the synclock the the imagelist but it had no effect on the
code, any ideas or work arounds? Maybe this is a known bug? (hopefully).

I'm going to have a few more hacks at it before I go to sleep I think,
isn't it typical how you come across a big problem when your just about
happy with it?

Nick.
 
Hi Nick,

Is the updateImageThumbnail called on the backgroud working thread?
If so, I think we would better masharl the call on the main UI thread, it
is not recommend to modity UI control's property in the thread other than
main UI thread.
You may try to use the control.BeginInvoke or control.Invoke method to
marshal the call back to the UI control's thread(main UI thread).

Control.Invoke Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscontrolclassinvoketopic.asp

Control.BeginInvoke Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemWindowsFormsControlClassBeginInvokeTopic.asp

Private Sub ChangeText()
Dim rd As New Random
Me.Button1.Text = rd.Next().ToString() 'These code will run on
button1's thread, Main UI thread.
End Sub
Private Sub ThreadProc()
For i As Integer = 0 To 10
Me.Button1.Invoke(New MethodInvoker(AddressOf ChangeText))
Thread.Sleep(1000)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim th As New Thread(AddressOf ThreadProc)
th.Start()
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Phillip,

Cheers, strange how this hadn't come up sooner, but thats programming I
suppose!

Nick.
 
Hi again,

While attempting to implementing this invoking thing, I found my
problem! The listview *was* being altered on another thread but nowhere
near where I was looking, sorry for all this! That's what working too late
does to your head!

Nick.
 
Hi Nick,

If you still have any concern on this issue, please feel free to post here.
Cheers!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

Naah, all sorted now, I'm still accessing it from a background thread
but without this capability it would destroy the speed of my thumbnail
browser. It works very well now anyway, no problems since finding this bug.

Nick.
 
Hi

I am glad that works for you.

Cheers!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top