DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. .NET 4.5 Baby Steps, Part 3: IDataFlowBlock

.NET 4.5 Baby Steps, Part 3: IDataFlowBlock

Robert Maclean user avatar by
Robert Maclean
·
May. 10, 12 · Interview
Like (0)
Save
Tweet
Share
4.36K Views

Join the DZone community and get the full member experience.

Join For Free

introduction

a new interface in .net is the idataflowblock, which is implemented in many interesting ways, so to look at those we will start off with the simplest implementation. actionblock<tinput> is a completely new class in .net 4.5 and provides a way of working with data in a very task orientated way. i simplistically think of this as the implementation of the iobserver interface we got in .net 4. but do not limit your thinking to just that.

to use it, you first must add a reference to system.threading.tasks.dataflow

image

in this simple first example i am doing a fairly simple pub/sub demo:

var subscriber = new actionblock<string>(input =>
    {
        console.writeline("got: {0}", input);
    });

for (int i = 0; i < 10; i++)
{
    task.factory.startnew(() =>
        {
            thread.sleep(new random().next(200, 1000));
            subscriber.post(datetime.now.tolongtimestring());
        });
}

console.readline();
croppercapture[3]

as iobserver<t>

so the first fantastic feature is that it does have the ability (via extension method) to be an iobsserver<t> so it really solves the need to build up your own subscriber classes when implementing a pub/sub model.

first is the code for the publisher class – this is normal for the iobservable<t> as we had in .net 4. this just means our new code can play well with our existing code.

public class publisher : iobservable<string>
{
    list<iobserver<string>> subscribers = new list<iobserver<string>>();

    public idisposable subscribe(iobserver<string> observer)
    {
        subscribers.add(observer);
        return null;
    }

    public void send()
    {
        foreach (var item in subscribers)
        {
            item.onnext(datetime.now.tolongtimestring());
        }
    }
}

for our demo code, which produces the same as above:

var publisher = new publisher();

var subscriber = new actionblock<string>(input =>
    {
        console.writeline("got: {0}", input);
    });

publisher.subscribe(subscriber.asobserver());

for (int i = 0; i < 10; i++)
{
    task.factory.startnew(() =>
        {
            thread.sleep(new random().next(200, 1000));
            publisher.send();
        });
}

complete

the next awesome feature is the complete method which can be used to stop accepting of input when called – this is great for services where you want to shut down.

in this demo code it will run until you press enter:

var subscriber = new actionblock<string>(input =>
{
    console.writeline("got: {0}", input);
});


task.factory.startnew(() =>
{
    while (true)
    {

        thread.sleep(new random().next(200, 1000));
        subscriber.post(datetime.now.tolongtimestring());
    }
});

console.writeline("press any key to stop input");
console.readline();
subscriber.complete();
attachment size
actionblock<t> demos 8.36 kb
Interface (computing) Implementation Awesome (window manager) Task (computing) Data (computing) Build (game engine) .NET

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Front-End Troubleshooting Using OpenTelemetry
  • Create Spider Chart With ReactJS
  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: