I am attempting to programmatically remove items from the carousel listbox based upon other logic. I am coding the logic using the "tag" property of the item that i also programmatically added at runtime. When I attempt loop through the items and access the tag property i receive an error stating that "The calling thread cannot access this object because a different thread owns it".
I wanted to find out if this was caused by the carousel itself or something in my code. So, far I can't find the problem in my code and was wondering if items added to the carousel were contained in their own thread(s).
Thanks.
DependencyObjects in WPF (which includes all elements/controls) are tied to the thread on which they are created. The implementation of GetValue method of DependencyObject (which is used to read the dependencyproperty value) calls VerifyAccess which ensures that you are calling the method on the thread in which it was created so you must be asking for the value of that property from a different thread than the one on which the element was created. Perhaps you're doing this from a timer that raises its tick on a separate thread or from the BeginInvoke of a Delegate.
Thanks...
I have a delegate event that is firing to let me know to remove the item from the carousel... But... the error is when looping through the carousel items like the code below. The error is on the itm.tag object/property... This logic is in the same class that I added the item to the carousel (although a different method).
For Each itm In carouselLB.Items
If itm.tag = e.ExtID Then
carouselLearn.Items.Remove(itm)
Exit For
End If
Next
It doesn't really where the code is - it only matters who is executing the code and from what thread. As I mentioned, we are not throwing that error - it is being thrown by the WPF DependencyObject so it must be that you are executing that code from a separate thread. If this code is called from the BeginInvoke of a delegate then you are calling the code from a different thread. Maybe you should be using BeginInvoke of an element's Dispatcher instead since that will call you back asynchronously on the element's thread.
Thanks.... you're correct.