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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

.NET 4.5 Baby Steps, Part 3: IDataFlowBlock

Robert Maclean user avatar by
Robert Maclean
·
May. 10, 12 · · Interview
Like (0)
Save
Tweet
4.18K 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

  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • Implementing RBAC Configuration for Kubernetes Applications
  • Ultra-Fast Microservices: When Microstream Meets Payara
  • A Developer Evangelist's Thoughts on Angular 2

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo