StealthRT
StealthRT

Reputation: 10542

WPF hidding images

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?

Upvotes: 0

Views: 265

Answers (2)

Matt Whetton
Matt Whetton

Reputation: 6786

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 
    }
)

Upvotes: 1

Vishwaram Sankaran
Vishwaram Sankaran

Reputation: 357

try the link here

the below code should work fine :

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, _
New Action(Function() theImgs(intX).Visibility = Visibility.Hidden))

Upvotes: 0

Related Questions