Updating more than one property inside a timer eventhandler fails

Topics: Issues / bugs
Sep 1, 2012 at 7:19 PM

Hello Geert,

I am working on a wpf application.

Inside  a viewmodel I use a timer to update 3 properties periodically.
These properties are bound to controls in the view.
The timer triggers every 1000 ms.

void timer_Elapsed(object sender, ElapsedEventArgs e)
            {
            property1= NewValue1;
            property2= NewValue2;
            property3= NewValue3;
            }

Only property1 gets updated. All 3 new values are different compared to the old property values every time the timer triggers.
Lines 2 and 3 inside the event handler are not executed. A breakpoint on line 2 is never hit.

If I remove the change regarding "InvalidateCommandsOnPropertyChanged" made in Rev. 802 in ViewModelBase the event handler works as expected.

 

Sep 3, 2012 at 9:03 PM

Interesting, we will investigate this. Thanks for letting us know.

Maybe there is an exception in one of your commands?

Sep 3, 2012 at 9:06 PM

Can you also also check a new property we introduced. I think it was called DispatchPropertyChangeNotifications or something like that. Try setting that to true.

Sep 4, 2012 at 12:27 PM

At least this has been a threading problem.
Timer callback functions are executed on a different thread.

The following solved the problem:

void timer_Elapsed(object sender, ElapsedEventArgs e)
            {
            var dispatcherService = GetService<IDispatcherService>();
            dispatcherService.BeginInvoke(() => UpdateValues());
            }

void UpdateValues()
            {
            property1= NewValue1;
            property2= NewValue2;
            property3= NewValue3;
            }
Thanks for your help.

Sep 4, 2012 at 12:46 PM

Yes, then the DispatchPropertyChangedEvent is the property you want to use. It does this automatically for you.

Sep 4, 2012 at 1:14 PM

Hello Geert,

DispatchPropertyChangedEvent is ReadOnly.

How do you use that ?

Sep 4, 2012 at 1:18 PM

I am really sorry for that, should have been protected. I will fix that tonight.

Sep 5, 2012 at 7:54 AM

You can try the latest beta we released yesterday (via nuget).

Sep 5, 2012 at 8:55 AM

Thank you for the quick changes.
Unfortunately this doesn't solve the problem.

After setting DispatchPropertyChangedEvent to true in the constructor of the viewmodel the program seems to be deadlocked as soon as the first property is changed in the timer callback function.

After i changed the line

           _dispatcherService.Invoke(() => base.RaisePropertyChanged(sender, e));

in method RaisePropertyChanged in ViewModelbase to

           _dispatcherService.BeginInvoke(() => base.RaisePropertyChanged(sender, e));

everything works as expected.

Could this be an option ?

Sep 5, 2012 at 7:19 PM

Done, check out the new release candidate.

Sep 6, 2012 at 8:57 AM

Perfect.

Thanks for your quick help.