Click or drag to resize

NotifyDataChangedGetEventQueue Method

Namespace:  Demo3D.Net
Assembly:  Demo3D.IO (in Demo3D.IO.dll) Version: 18.03.00
Syntax
C#
public static NotifyDataChangedEventQueue GetEventQueue(
	this INotifyDataChanged notify
)

Parameters

notify
Type: Demo3D.NetINotifyDataChanged
The INotifyDataChanged interface.

Return Value

Type: NotifyDataChangedEventQueue
A NotifyDataChangedEventQueue object that will store and forward data changed events.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type INotifyDataChanged. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Remarks

Given an INotifyDataChanged interface, this will return a NotifyDataChangedEventQueue that has subscribed to the INotifyDataChanged.DataChanged event. The NotifyDataChangedEventQueue 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(). GetEventQueue is mainly aimed at services that implement INotifyDataChanged but have no blocking Read(Async) method (or similar).

Examples
C#
// Reacts to data changing.
// For services such as IMemoryService or ITagService that don't have a Read method (or similar).
// GetEventQueue returns an object that will watch for INotifyDataChanged.DataChanged events
// and return each one as they occur.
public async Task ReactToDataChanging(bool sync, INotifyDataChanged service) {
    using (var notifier = service.GetEventQueue()) {
        for (;;) {
            foreach (var data in await notifier.ReadEventsAsync(sync).ConfigureAwait(false)) {
                Logger.Log("Data changed");
            }
        }
    }
}

// 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 Task ReactToDataChanging(IPacketIOService service) {
    for (;;) {
        var data = await service.ReadAsync(length: -1);  // -1 means read all the available data
        Logger.Log("Data changed");
    }
}
See Also