I have a problem with threads and dispatcher in wpf, I created a thread which in certain cases bind class(wpf page control) to Main.MainContent.Content, only works the first time, then it doesn't work. I am a beginner in this topic, so I am asking for understanding and a thorough explanation. Let me add that I read a bit about DispatcherPriority Enum and tried to replace BeginInvoke with Invoke.
Asked
Active
Viewed 112 times
0
-
Okay, you have a problem. But what is your question? – Thomas Weller Oct 05 '21 at 10:44
-
I want this thread to be executed every time, not just the first, after the first execution, when the program reaches the dispatcher it doesn't work. – Daniel2001k Oct 05 '21 at 10:48
-
Check explanation between Invoke and BeginInvoke: https://stackoverflow.com/questions/19009174/dispatcher-invoke-vs-begininvoke-confusion – Auditive Oct 05 '21 at 10:51
-
I checked again, added thread.sleep (1000) and checked begininvoke and invoke and still don't work – Daniel2001k Oct 05 '21 at 11:07
-
Why are you invoking, while on the dispatcher thread it self? – Jeroen van Langen Oct 05 '21 at 11:14
-
Please do not post images of code or errors. Include it in text with code formatting instead. – JonasH Oct 05 '21 at 11:14
-
How else can I change content from a thread? – Daniel2001k Oct 05 '21 at 11:16
-
Whats wrong with a `DispatcherTimer`? – Jeroen van Langen Oct 05 '21 at 11:17
-
1If you want to work from a thread, you have to prepare the data in classes and assign them later via a Dispatcher.Invoke – Jeroen van Langen Oct 05 '21 at 11:18
1 Answers
0
I solved my problem by changing one line:
MainWindow.Main.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { MainWindow.Main.MainContent.Content = new AcceptedPage(); }));
on:
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { Application.Current.MainWindow.Content = new AcceptedPage(); }));

Daniel2001k
- 3
- 4