David Brunelle
David Brunelle

Reputation: 6440

WPF : Accessing a UI object with a Thread gives an error

I have a function that is called when a file is modified in a folder. This function updates the items of a listview which is stored in the UI.

Here is the function :

Private Sub FileChangeNotify()
    Try
        LstMoulures.Items.Refresh()
    Catch ex As Exception
        MsgBox(Ex.exception)
    End Try

End Sub

Here is the error: "Le thread appelant ne peut pas accéder à cet objet parce qu'un autre thread en est propriétaire." (Translation : The calling thread cannot access this object because it is owned by another thread)

Thanks.

Upvotes: 1

Views: 4719

Answers (2)

agent-j
agent-j

Reputation: 27913

Private Sub FileChangeNotify()
  LstMoulures.Dispatcher.BeginInvoke (New Action(AddressOf LstMoulures.Items.Refresh))
End Sub

Upvotes: 1

Nick B
Nick B

Reputation: 1101

You cannot modify objects in the UI thread directly from another thread - check out the BeginInvoke method, which lets you access UI objects across threads.

Check out WPF C# - Editing a listbox from another thread

Upvotes: 3

Related Questions