Reputation: 5452
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.
Upvotes: 7
Views: 7445
Reputation: 4673
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
Upvotes: 1
Reputation: 244757
One way to do this is awaiting Task.Delay()
in a loop instead of using a timer:
class MyTimer : INotifyPropertyChanged
{
public MyTimer()
{
Start();
}
private async void Start()
{
while (true)
{
await Task.Delay(TimeSpan.FromSeconds(1));
PropertyChanged(this, new PropertyChangedEventArgs("Time"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public DateTime Time { get { return DateTime.Now; } }
}
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).
Upvotes: 5
Reputation: 33379
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 proper SynchronizationContext
on which to communicate. To do this you need to capture the current SynchronizationContext
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:
public class MyTimer
{
private SynchronizationContext synchronizationContext;
public MyTimer() : this(SynchronizationContext.Current)
{
}
public MyTimer(SynchronizationContext synchronizationContext)
{
if(this.synchronizationContext == null)
{
throw new ArgumentNullException("No synchronization context was specified and no default synchronization context was found.")
}
TimerElapsedHandler f = new TimerElapsedHandler(NotifyTimeChanged);
TimeSpan period = new TimeSpan(0, 0, 1);
ThreadPoolTimer.CreatePeriodicTimer(f, period);
}
private void NotifyTimeChanged()
{
if(this.PropertyChanged != null)
{
this.synchronizationContext.Post(() =>
{
this.PropertyChanged(this, new PropertyChangedEventArgs("Time"));
});
}
}
}
Upvotes: 7