Simpliest code: Can't change label "Content" programmatically
-
Hello everyone. Currently I am trying to work with timers in WPF.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_timer = new System.Timers.Timer();
_timer.Interval = 1000;
_timer.Elapsed += OnTimerElapsed;
_timer.Start();} int i=0; private void OnTimerElapsed(object sender, ElapsedEventArgs e) { i++; label1.Content = i.ToString(); }
I've got an exception at this string
label1.Content = i.ToString();
What I'm doing wrong with a label? Thanks in advance! XAML:
-
Hello everyone. Currently I am trying to work with timers in WPF.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_timer = new System.Timers.Timer();
_timer.Interval = 1000;
_timer.Elapsed += OnTimerElapsed;
_timer.Start();} int i=0; private void OnTimerElapsed(object sender, ElapsedEventArgs e) { i++; label1.Content = i.ToString(); }
I've got an exception at this string
label1.Content = i.ToString();
What I'm doing wrong with a label? Thanks in advance! XAML:
Pew_new wrote:
What I'm doing wrong with a label?
You access it from a different thread - you must not do that, and that holds true also for other UI technologies like WinForms. With WPF also comes MVVM. Instead of setting the Content property directly, you bind it to some property of your view model. When that property changes, you raise an PropertyChanged event, and that will do the magic of "transporting" the change in the correct thread.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
-
Pew_new wrote:
What I'm doing wrong with a label?
You access it from a different thread - you must not do that, and that holds true also for other UI technologies like WinForms. With WPF also comes MVVM. Instead of setting the Content property directly, you bind it to some property of your view model. When that property changes, you raise an PropertyChanged event, and that will do the magic of "transporting" the change in the correct thread.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
Please explain where have you seen " different thread"? "different thread", you mean that with a System.Timer it's not possible? P.S. Here https://www.youtube.com/watch?v=QkT8fgoFz3g, a guy is doing that easily. True he's using a Dispatcher Timer. So what's difference?
-
Please explain where have you seen " different thread"? "different thread", you mean that with a System.Timer it's not possible? P.S. Here https://www.youtube.com/watch?v=QkT8fgoFz3g, a guy is doing that easily. True he's using a Dispatcher Timer. So what's difference?
The
DispatcherTimer
documentation makes things slightly clearer than theSystem.Timers.Timer
documentation:DispatcherTimer Class (System.Windows.Threading) | Microsoft Docs[^]:
If a
System.Timers.Timer
is used in a WPF application, it is worth noting that theSystem.Timers.Timer
runs on a different thread than the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto theDispatcher
of the user interface (UI) thread usingInvoke
orBeginInvoke
. Reasons for using aDispatcherTimer
as opposed to aSystem.Timers.Timer
are that theDispatcherTimer
runs on the same thread as theDispatcher
and aDispatcherPriority
can be set on theDispatcherTimer
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer