The timer callback is executed on a thread that is not a user interface thread, which causes problems; the reason this happens despite the fact that you are βonly updating the propertyβ is because such a call creates a chain of calls, namely to notify interested parties about changes to the property, which in this case is the user interface, and, consequently, an area of ββbubbles to cause incorrect calls with cross flows.
To overcome this, you can specify the Dispatcher your Window in the timer constructor as an argument to the state parameter, then use Dispatcher.Invoke .
For instance...
public MainViewModel() { _dateTimer = new Timer(_dateTimer_Tick, Dispatcher, 0, 60000); } void _dateTimer_Tick(object state) { ((Dispatcher)state).Invoke(UpdateUI); } void UpdateUI() { Time = DateTime.Now.ToString("HH:mm"); Date = DateTime.Now.ToString("D"); }
EDIT:
Using a DispatcherTimer , as suggested by Henk, and even considered by yourself, may well be the way to go here, however - I simply did not know about the type and therefore could not demonstrate in my answer. Regarding DispatcherTimer and performance, on what basis is your concern based?
source share