private async Task UpdateItemsAsync(CancellationToken token) { token.ThrowIfCancellationRequested(); var newValue = await _plc.ReadItemAsync(_placeholderItem, token); // Make a copy of all monitored plc items in a thread safe manner. token.ThrowIfCancellationRequested(); List <IPlcItem> plcItems; lock (_lock) { plcItems = new List <IPlcItem>(_plcItems); } // Update the value of the monitored items. //foreach (var plcItem in plcItems) //{ // token.ThrowIfCancellationRequested(); // plcItem.Value.TransferValuesFrom(newValue); //} Parallel.ForEach ( plcItems, (plcItem) => { token.ThrowIfCancellationRequested(); plcItem.Value.TransferValuesFrom(newValue); } ); }
/// <summary> /// Reads the value of the <paramref name="plcItem"/> from the plc. /// </summary> /// <param name="plc"> The extended <see cref="IPlcItem"/> instance. </param> /// <typeparam name="TValue"> The type of the <see cref="IPlcItem{TValue}.Value"/>. </typeparam> /// <param name="plcItem"> The <see cref="IPlcItem{TValue}"/> to read. </param> /// <param name="cancellationToken"> An optional <see cref="CancellationToken"/> for cancelling the read operation. </param> /// <returns> An awaitable task containing the result as <typeparamref name="TValue"/>. </returns> /// <exception cref="ReadPlcException"> Thrown if an exception occurred while reading. </exception> public static async Task <TValue> ReadItemAsync <TValue>(this IPlc plc, IPlcItem <TValue> plcItem, CancellationToken cancellationToken = default) { await plc.ReadItemAsync(plcItem as IPlcItem, cancellationToken); return(plcItem.Value); }