Results 1 to 10 of 10

Thread: [RESOLVED] Thread Safe ListView

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Resolved [RESOLVED] Thread Safe ListView

    Hi All,

    I currently have a form with a listview. On another form, I would like get all the items from that listview. This I can achieve but I always get the "Cross-Thread operation not allowed exception" message.

    I've tried the following code to avail. Any suggestions?

    vb Code:
    1. Delegate Sub ListView_Delegate(ByVal [tslvw] As ListView)
    2.  
    3.     Private Sub ListView_ThreadSafe(ByVal tslvw As ListView)
    4.  
    5.         If [tslvw].InvokeRequired Then
    6.             Dim MyDelegate As New ListView_Delegate(AddressOf ListView_ThreadSafe)
    7.             Me.Invoke(MyDelegate, New Object() {[tslvw]})
    8.         End If
    9.  
    10.     End Sub

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

    Re: Thread Safe ListView

    Your method doesn't do anything if an invocation is NOT required. The idea is that if InvokeRequired is True you call Invoke, otherwise you do the work. You've got the first part but not the second. You might want to follow the CodeBank link in my signature and find my Controls & Multithreading submission, which is dedicated to this topic.
    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
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Thread Safe ListView

    As well as what JMC has said about correcting the way your code works, I would also question whether or not you actually need to pass in the entire listview object. If you just need to access the items then pass in the Items property of the ListView.

    Also, just accessing the items of a listview from another form should not cause cross-thread exceptions unless your calling code is being run from a background thread. You say you just want to get these items "from another form" so I'm wondering if you are actually showing that form on a background thread (which is generally not a good idea)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: Thread Safe ListView

    Quote Originally Posted by chris128 View Post
    If you just need to access the items then pass in the Items property of the ListView.
    You can't do that from another thread though, which is the whole point.
    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

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Thread Safe ListView

    I know - I didnt say just pass the items property of the listview and that will solve your threading problems... I just said as well as following your guide for how to access controls from a worker thread, if you only need to access the items then why bother passing the entire listview.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: Thread Safe ListView

    Quote Originally Posted by chris128 View Post
    I know - I didnt say just pass the items property of the listview and that will solve your threading problems... I just said as well as following your guide for how to access controls from a worker thread, if you only need to access the items then why bother passing the entire listview.
    I think you'll find that that method from the first post is the one that's being called from the secondary thread, so all you can pass is the control itself. The whole point of that method is to use that parameter as a way to access members of the control in a thread safe way. You can't pass the Items property to a method that is being called in a secondary thread. I think what you may be missing is the fact that that method is simply not going to work as is. It needs to be rewritten as a function that actually returns the items. Basically you pass in a reference to the ListView, delegate to the UI thread, get the items and return them, which will return them to the original thread via the delegate.
    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

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Thread Safe ListView

    Yeah thats what I meant but I guess I didnt explain that it needed to be rewritten, my bad.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: Thread Safe ListView

    Quote Originally Posted by chris128 View Post
    my bad.
    Possibly my dumb, but I'll go with your bad.
    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

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Thread Safe ListView

    Touché
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Talking Re: Thread Safe ListView

    Hi All,

    Thanks for the suggestions. I ended up saving each listview item into a collection. I then passed that collection across to the other form for processing.

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