Results 1 to 4 of 4

Thread: Invoking ListView problem....

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Invoking ListView problem....

    Hello,

    This is my code:

    vb.net Code:
    1. Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Me.FolderBrowserDialog1.ShowDialog()
    3.  
    4.         If Me.FolderBrowserDialog1.SelectedPath <> String.Empty Then
    5.             Me.TextBox2.Text = Me.FolderBrowserDialog1.SelectedPath
    6.             Dim deleg As New Thread(AddressOf UpdateFileList)
    7.             deleg.Start()
    8.         End If
    9.  
    10.         Me.FolderBrowserDialog1.Reset()
    11.     End Sub
    12. #End Region
    13.  
    14.     Private Sub UpdateFileList()
    15.         Try
    16.             Dim _filenames As ReadOnlyCollection(Of String) = My.Computer.FileSystem. _
    17.             GetFiles(Me.TextBox2.Text, FileIO.SearchOption.SearchAllSubDirectories)
    18.  
    19.             For Each filename As String In _filenames
    20.                 Me.ImageList1.Images.AddStrip(ExtractAssociatedIcon(filename).ToBitmap)
    21.                 If ListView1.InvokeRequired Then
    22.                     Me.ListView1.Invoke(New UpdateProgressCallBack(AddressOf UpdateFileList))
    23.                 Else
    24.                     ListView1.Items. _
    25.                     Add(filename, imageIndex:=Me.ImageList1.Images.Count - 1) _
    26.                     .SubItems.Add(filename)
    27.                 End If
    28.             Next
    29.         Catch ex as Exception
    30.  
    31.         End Try
    32.     End Sub


    If I wanted to add 3 icons, the LV would add them like it normally would, but then, for some reason, it enters the loop again and that's when it throws this error:

    ex = {"Cross-thread operation not valid: Control 'ListView1' accessed from a thread other than the thread it was created on."}

    I don't understand why, because I'm invoking the LV...

    Thanks in advance!
    Last edited by tassa; Oct 3rd, 2009 at 01:56 AM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width