0

I am trying to hide all my images i currently have on my WPF using this code below:

Dim theImgs() As Controls.Image = {picNextTopic1, picNextTopic2, picNextTopic3, picNextTopic4, picNextTopic5, picNextTopic6, picNextTopic7, picNextTopic8, picNextTopic9, picNextTopic10, picNextTopic11, picNextTopic12, picNextTopic13, picNextTopic14, picNextTopic15, picNextTopic16}

Dim intX As Integer = 0

Do Until intX = theImgs.Length
   Try
      theImgs(intX).Visibility = Visibility.Hidden
      intX += 1
   Catch ex As Exception
      MsgBox(ex.Message)
   End Try
Loop

However, when running the code above i get this error:

The calling thread cannot access this object because a different thread owns it

How can i fix this error?

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • 1
    Most likely either the image was created on a background thread, or you're trying to modify it from a background thread. As a solution, use the `Dispatcher` instead of a background thread to run the code on the main UI thread of the application. – Rachel Dec 19 '13 at 16:07
  • @Rachel mind showing an example? – StealthRT Dec 19 '13 at 16:08
  • Sure, check out [this answer](http://stackoverflow.com/a/8759160/302677). Its for updating a non-UI object, but the problem and solution is still the same. – Rachel Dec 19 '13 at 16:12
  • @Rachel I'm new to WPF so thats a lot of stuff on that page im not sure where to put and all. The images are already on the WPF form so im not sure why its saying its in a differnt thread... – StealthRT Dec 19 '13 at 16:19
  • Does this code run from a new thread or BackgroundWorker? If so, you need to send the code to update the Images to the UI thread using the `Dispatcher`, because a background thread cannot update the properties of an object it did not create. If not, then the `theImgs` collection was created on a separate thread, and the logic of the code needs to change to create them from the main UI thread. – Rachel Dec 19 '13 at 18:27
  • @Rachel only thing i know is i have 16 images on the form and i am just trying to hide them all with the code i posted in my OP when the *FileSystemWatcher* finds a change it calls the function this code is in. – StealthRT Dec 19 '13 at 18:36
  • The `SystemFileWatcher` is probably kicking off this code on a background thread then. I'd suggest using the `Dispatcher` to ensure the code runs on the main thread. There are a couple of answers below (You shouldn't need to specify the `DispatcherPriority` like Vishwaram's answer has), or you can find many more examples online, such as [this example](http://stackoverflow.com/a/19109948/302677) :) – Rachel Dec 19 '13 at 20:55

2 Answers2

1

Change:

theImgs(intX).Visibility = Visibility.Hidden;

To:

C#

Application.Current.Dispatcher.Invoke(new Action(() => 
    {
        theImgs[intX].Visibility = Visibility.Hidden; 
    });

VB

Application.Current.Dispatcher.Invoke(
    Function(){ 
        theImgs(intX).Visibility = Visibility.Hidden 
    }
)
Matt Whetton
  • 6,616
  • 5
  • 36
  • 57
  • Cant seem to convert that into VB.net? – StealthRT Dec 19 '13 at 17:43
  • I tried **Application.Current.Dispatcher.Invoke(Function(){ theImgs(intX).Visibility = Visibility.Hidden})** but that doesnt seem to work? – StealthRT Dec 19 '13 at 18:18
  • And i have also tried **Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, New Action(Function() {theImgs(intX).Visibility = Visibility.Hidden}))** and that also does not work. – StealthRT Dec 19 '13 at 18:22
  • It sounds like the dispatcher has sorted your first exception, but there must be a another problem. Can you post your xaml? – Matt Whetton Dec 23 '13 at 11:41
0

try the link here

the below code should work fine :

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, _
New Action(Function() theImgs(intX).Visibility = Visibility.Hidden))
Community
  • 1
  • 1
  • I get the error **Object reference not set to an instance of an object.** – StealthRT Dec 19 '13 at 18:34
  • try the below code: Dispatcher di = Dispatcher.CurrentDispatcher;// Next to InitiliazeComponent(); Store the Di variable at class level Now try using di.Invoke(DispatcherPriority.Background, _ New Action(Function() theImgs(intX).Visibility = Visibility.Hidden)) – Vishwaram Sankaran Dec 20 '13 at 04:53