|
-
Sep 16th, 2009, 03:01 AM
#1
Thread Starter
Lively Member
[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:
Delegate Sub ListView_Delegate(ByVal [tslvw] As ListView)
Private Sub ListView_ThreadSafe(ByVal tslvw As ListView)
If [tslvw].InvokeRequired Then
Dim MyDelegate As New ListView_Delegate(AddressOf ListView_ThreadSafe)
Me.Invoke(MyDelegate, New Object() {[tslvw]})
End If
End Sub
-
Sep 16th, 2009, 03:15 AM
#2
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.
-
Sep 16th, 2009, 03:25 AM
#3
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)
-
Sep 16th, 2009, 05:32 AM
#4
Re: Thread Safe ListView
 Originally Posted by chris128
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.
-
Sep 16th, 2009, 06:16 AM
#5
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.
-
Sep 16th, 2009, 08:13 AM
#6
Re: Thread Safe ListView
 Originally Posted by chris128
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.
-
Sep 16th, 2009, 08:23 AM
#7
Re: Thread Safe ListView
Yeah thats what I meant but I guess I didnt explain that it needed to be rewritten, my bad.
-
Sep 16th, 2009, 09:37 AM
#8
Re: Thread Safe ListView
 Originally Posted by chris128
my bad.
Possibly my dumb, but I'll go with your bad.
-
Sep 16th, 2009, 09:49 AM
#9
-
Sep 16th, 2009, 09:58 PM
#10
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|