0

My WPF app polls some external hardware and shows data changes using binding to INotifyPropertyChanged. It works great so far but I would like it to pause polling when controls that show the data are "dead" and continue when they're on-screen again.

How do I change the data source object (which at the moment is just an INotifyPeropertyChanged implementation) so that it knows when target controls go unloaded? I could make it wait for targets to get their properties in a certain time span after corresponding PropertyChanged events, but may be you have a better suggestion.

Yegor
  • 2,514
  • 2
  • 19
  • 27
  • 1
    What do you mean with "dead"? The controls/bindings normally register and unregister from the PropertyChangedEvent as needed. – Jehof May 21 '15 at 06:27
  • Say, I have two Windows with DataContext set to an INotifyPropertyChanged object. Inside those windows are two different sets of controls bound to different properties of that object. If either window gets closed, I need to stop polling data for its controls to save bandwidth. – Yegor May 21 '15 at 06:45
  • I've just tried custom event accessors and the only relevant object in the 'value' was an instance of PropertyChangedEventManager that doesn't seem to provide useful information on binding targets. – Yegor May 21 '15 at 06:51
  • Add a property like IsActive to your DataContext that is bound to the Visibility or OpenState of the Windows. Maybe you need a converter. Then when IsActive is toggled you start/stop your polling – Jehof May 21 '15 at 06:54

1 Answers1

0

If either window gets closed, I need to stop polling data for its controls to save bandwidth

However the polling works, you need to create a flag, maybe on your VM, which will identify whether operations should be running or not. The polling needs to also poll that flag to run or not depending on the state of it/the windows.

Subscribe to the close event of the window and accordingly update the flag which will subsequently stop the polling.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122