Results 1 to 4 of 4

Thread: Invoking ListView problem....

  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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Invoking ListView problem....

    You've structured the code incorrectly. If you're invoking the same method then the test for whether or not an invocation is required needs to be the FIRST thing you do, otherwise everything you've already done is going to be repeated when you do invoke. Your code should be like this:
    vb.net Code:
    1. Private Sub UpdateFileList()
    2.     Try
    3.         If ListView1.InvokeRequired Then
    4.             Me.ListView1.Invoke(New UpdateProgressCallBack(AddressOf UpdateFileList))
    5.         Else
    6.             Dim _filenames As ReadOnlyCollection(Of String) = My.Computer.FileSystem. _
    7.             GetFiles(Me.TextBox2.Text, FileIO.SearchOption.SearchAllSubDirectories)
    8.  
    9.             For Each filename As String In _filenames
    10.                 Me.ImageList1.Images.AddStrip(ExtractAssociatedIcon(filename).ToBitmap)
    11.                 ListView1.Items. _
    12.                 Add(filename, imageIndex:=Me.ImageList1.Images.Count - 1) _
    13.                 .SubItems.Add(filename)
    14.             Next
    15.         End If
    16.     Catch ex As Exception
    17.  
    18.     End Try
    19. End Sub
    If you want to do everything on the UI thread. I'm guessing that you don't want that so you need two methods, like this:
    vb.net Code:
    1. Private Sub UpdateFileList()
    2.     Try
    3.         Dim _filenames As ReadOnlyCollection(Of String) = My.Computer.FileSystem. _
    4.         GetFiles(Me.TextBox2.Text, FileIO.SearchOption.SearchAllSubDirectories)
    5.  
    6.         For Each filename As String In _filenames
    7.             Me.ImageList1.Images.AddStrip(ExtractAssociatedIcon(filename).ToBitmap)
    8.             Me.UpdateListView(filename)
    9.         Next
    10.     Catch ex As Exception
    11.  
    12.     End Try
    13. End Sub
    14.  
    15. Private Delegate Sub UpdateListViewCallback(ByVal fileName As String)
    16.  
    17. Private Sub UpdateListView(ByVal fileName As String)
    18.     If ListView1.InvokeRequired Then
    19.         Me.ListView1.Invoke(New UpdateListViewCallback(AddressOf UpdateListView), fileName)
    20.     Else
    21.         ListView1.Items. _
    22.         Add(fileName, imageIndex:=Me.ImageList1.Images.Count - 1) _
    23.         .SubItems.Add(fileName)
    24.     End If
    25. End Sub
    or like this:
    vb.net Code:
    1. Private Sub UpdateFileList()
    2.     Try
    3.         Dim _filenames As ReadOnlyCollection(Of String) = My.Computer.FileSystem. _
    4.         GetFiles(Me.TextBox2.Text, FileIO.SearchOption.SearchAllSubDirectories)
    5.  
    6.         Me.UpdateListView(_filenames)
    7.     Catch ex As Exception
    8.  
    9.     End Try
    10. End Sub
    11.  
    12. Private Delegate Sub UpdateListViewCallback(ByVal fileNames As ReadOnlyCollection(Of String))
    13.  
    14. Private Sub UpdateListView(ByVal fileNames As ReadOnlyCollection(Of String))
    15.     If ListView1.InvokeRequired Then
    16.         Me.ListView1.Invoke(New UpdateListViewCallback(AddressOf UpdateListView), fileNames)
    17.     Else
    18.         For Each filename As String In fileNames
    19.             Me.ImageList1.Images.AddStrip(ExtractAssociatedIcon(filename).ToBitmap)
    20.             Me.UpdateListView(filename)
    21.             ListView1.Items. _
    22.             Add(filename, imageIndex:=Me.ImageList1.Images.Count - 1) _
    23.             .SubItems.Add(filename)
    24.         Next
    25.     End If
    26. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

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

    Re: Invoking ListView problem....

    Thanks! That works perfectly! One more question though: the third code runs the first sub on the worker thread and the second one on the UI thread?
    "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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Invoking ListView problem....

    In both the second and third cases the UpdateListView method is called on the secondary thread and then it is invoked a second time via the delegate on the UI thread. The UpdateListView method essentially calls itself with the second call being made on the UI thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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