Results 1 to 3 of 3

Thread: [RESOLVED] Return listview item collection in multithread?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Resolved [RESOLVED] Return listview item collection in multithread?

    I am doing this within a thread:
    Code:
     For Each item As ListViewItem In lvUsers.Items
    and I am getting: Cross-thread operation not valid: Control 'lvUsers' accessed from a thread other than the thread it was created on.

    I have been able to adjust a control in a way using something like:
    Code:
     Public Delegate Sub ListAddNameInvoker(ByVal text As String)
        Public Sub ListAddName(ByVal text As String)
            If lvUsers.InvokeRequired Then
                lvUsers.Invoke(New ListAddNameInvoker(AddressOf ListAddName), text)
            Else
                lvUsers.Items.Add(text)
            End If
        End Sub
    But how do I get data from a control and pass it to another thread?

  2. #2
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Return listview item collection in multithread?

    Check out this thread, might help you out .
    "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

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

    Re: Return listview item collection in multithread?

    You can't get data from a control in other than the UI thread. As such, you will need to get all the data you require out of the ListView first, into an array or collection, and then pass that data to the method that you execute on the secondary thread. For one way of doing that check out this. You might also use a BackgroundWorker and pass an argument to the RunWorkerCompleted method, or you might also use the ThreadPool and pass a second argument to the QueueUserWorkItem method.
    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