.NET Framework Parallel Dataflow, Part 3: Listing 1

UiUpdateBlock.cs

using System;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Windows.UI.Xaml.Controls;

namespace VSMDataflowPart3
{
    public class UiUpdateBlock<T> : IPropagatorBlock<T, T>
    {
        private readonly ITargetBlock<T> _target;
        private readonly ISourceBlock<T> _source;

        public UiUpdateBlock(TextBlock element)
        {
            _target = new ActionBlock<T>(x => element.Text = x.ToString(),
                new ExecutionDataflowBlockOptions() 
                { TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext() });
            _source = new BroadcastBlock<T>(x => x);
        }

        public DataflowMessageStatus OfferMessage(DataflowMessageHeader messageHeader,
            T messageValue, ISourceBlock<T> source, bool consumeToAccept)
        {
            return _target.OfferMessage(messageHeader, messageValue,
                source, consumeToAccept);
        }

        public void Complete()
        {
            _target.Complete();
        }

        public Task Completion
        {
            get { return _source.Completion; }
        }

        public void Fault(Exception exception)
        {
            _target.Fault(exception);
        }

        public T ConsumeMessage(DataflowMessageHeader messageHeader,
            ITargetBlock<T> target, out bool messageConsumed)
        {
            return _source.ConsumeMessage(messageHeader,
                target, out messageConsumed);
        }

        public IDisposable LinkTo(ITargetBlock<T> target, DataflowLinkOptions linkOptions)
        {
            return _source.LinkTo(target, linkOptions);
        }

        public void ReleaseReservation(DataflowMessageHeader messageHeader, ITargetBlock<T> target)
        {
            _source.ReleaseReservation(messageHeader, target);
        }

        public bool ReserveMessage(DataflowMessageHeader messageHeader, ITargetBlock<T> target)
        {
            return _source.ReserveMessage(messageHeader, target);
        }
    }
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube