KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Im working on a WPF application. I have a label called "Status_label" in MainWindow.xaml . and I want to change its content from a different class (signIn.cs). Normally I'm able to do this var mainWin = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow; mainWin.status_lable.Content = "Irantha signed in"; But my problem is,when I'm trying to access it via different thread in signIn.cs class, it gives an error: The calling thread cannot access this object because a different thread owns it. Can I solve this by using Dispatcher.Invoke(new Action(() =>{.......... or something else? EDIT: I'm gonna call this label change action from different class as-well-as separate thread MainWindow.xaml <Label HorizontalAlignment="Left" Margin="14,312,0,0" Name="status_lable" Width="361"/> SignIn.cs internal void getStudentAttendence() { Thread captureFingerPrints = new Thread(startCapturing); captureFingerPrints.Start(); } void mySeparateThreadMethod() { var mainWin = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow; mainWin.status_lable.Dispatcher.Invoke(new Action(()=> mainWin.status_lable.Content ="Irantha signed in")); } line var mainWin return error The calling thread cannot access this object because a different thread owns it. Please guide me, Thank you
Tags (comma-separated)
Save Edits
Cancel