NotifyDataChangedGetDataChangedNotifier Method |
Namespace: Demo3D.Net
public static NotifyDataChangedDataChangedNotifier GetDataChangedNotifier( this INotifyDataChanged notify )
Given an INotifyDataChanged interface, this will return a DataChangedNotifier that has subscribed to the INotifyDataChanged.DataChanged event. The DataChangedNotifier collects data changed events and delivers them to the caller on request.
This method facilitates code that can use a construct, such as a while loop, to wait for and respond to data changes.
For message streams (such as IPacketIOService), it's more efficient simply to loop calling IPacketIOService.ReadAsync(). GetDataChangedNotifier is mainly aimed at services that implement INotifyDataChanged but have no blocking Read(Async) method (or similar).
// Reacts to data changing. // For services such as IMemoryService or ITagService that don't have a Read method (or similar). // GetDataChangedNotifier returns an object that will watch for INotifyDataChanged.DataChanged events // and return each one as they occur. public async void ReactToDataChanging(INotifyDataChanged service) { using (var notifier = service.GetDataChangedNotifier()) { for (;;) { foreach (var data in await notifier.WhenDataChangedAsync()) { Logger.Log("Data changed at time " + data.Time); } } } } // Reacts to data changing. // For services such as IPacketIOService that do have a Read method, it's simpler and cheaper // just to Read the data directly. The effect is the same. public async void ReactToDataChanging(IPacketIOService service) { for (;;) { var data = await service.ReadAsync(); Logger.Log("Data changed at time " + DateTime.Now); } }