I am writing a Windows Store App toy application for Windows 8.
It has just one xaml page with a TextBlock
. The page has the class MyTimer as DataContext
:
this.DataContext = new MyTimer();
MyTimer
implements INotifyPropertyChanged
and the updating of the property Time
is made with a timer:
public MyTimer(){
TimerElapsedHandler f = new TimerElapsedHandler(NotifyTimeChanged);
TimeSpan period = new TimeSpan(0, 0, 1);
ThreadPoolTimer.CreatePeriodicTimer(f, period);
}
with
private void NotifyTimeChanged(){
if (this.PropertyChanged != null){
this.PropertyChanged(this, new PropertyChangedEventArgs("Time"));
}
}
the TextBlock
has a databinding on Time
<TextBlock Text="{Binding Time}" />
When I run the application i have the following exception:
System.Runtime.InteropServices.COMException was unhandled by user code
With the message
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
The real problem is that I am updating the property of the class MyTimer, not the GUI itself, I can't figure it out, but I think the solution should use something like this one.
One way to do this is awaiting
Task.Delay()
in a loop instead of using a timer:If you call the constructor on the UI thread, it will invoke the
PropertyChanged
there too. And the nice thing is that exactly the same code will work for example in WPF too (under .Net 4.5 and C# 5).how about the code from this blog:
http://metrowindows8.blogspot.in/2011/10/metro-tiles.html
This worked for me. I had to pass a ThreadPoolTimer object to my delegate function
Yes, you're notifying property changes from a thread pool thread rather than the UI thread. You need to marshal the notification back to the UI thread in the timer callback. Now, your view model is separated from your view (a good thing) therefore it doesn't have a direct link to the
Dispatcher
infrastructure. So what you want to do is hand it the properSynchronizationContext
on which to communicate. To do this you need to capture the currentSynchronizationContext
during construction or allow it to be passed in explicitly to a constructor which is good for tests or if you're initializing the object off the UI thread to begin with.The whole shebang would look something like this: