Reactive extensions Learning

Source: Internet
Author: User

Today, I took the time to learn the reactive extensions library. I thought it was easy to get started. I practiced it and wrote a readasync extension.

Static void main (string [] ARGs)
{
VaR buffersize = 1000;
VaR buffer = new byte [buffersize];

VaR stream = file. openread (@ "R: \ 1.txt ");

Stream. readasync (buffer). subscribe (
Onnext: I =>{ console. Write (encoding. Default. getstring (buffer, 0, I ));},
Oncompleted: () => console. writeline ("completed ")
);

Console. Readline ();
}

Public static iobservable <int> readasync (this stream, byte [] buffer)
{
VaR bufferlen = buffer. length;
VaR readasync = observable. fromasyncpattern <byte [], Int, Int, int> (stream. beginread, stream. endread );
Return observable. Defer () => readasync (buffer, 0, bufferlen). Repeat (). takewhile (I => I> 0)

// Return observable. While () => stream. Position! = Stream. length, observable. Defer () => readasync (buffer, 0, bufferlen )));
}

Public static iobservable <string> readlinesasync (this stream, byte [] buffer)
{
Return observable. createwithdisposable <string> (Observer =>
{
VaR blocks = stream. readasync (buffer). Select (I => encoding. Default. getstring (buffer, 0, I ));
VaR lastline = string. empty;

Return blocks. subscribe (Data =>
{
If (data. Contains ("\ r \ n "))
{
VaR lines = data. Split (New String [] {"\ r \ n"}, stringsplitoptions. None );

Observer. onnext (lastline + lines [0]);
Foreach (VAR line in lines. Skip (1). Take (lines. Length-2 ))
{
Observer. onnext (line );
}

Lastline = lines [lines. Length-1];
}
Else
{
Lastline + = data;
}
},
Observer. onerror,
() =>
{
Observer. onnext (lastline );
Observer. oncompleted ();
});
});
}

Of course, after C #5.0, with the support of the native syntax sugar of the compiler, implementing this extension is simpler and more intuitive, but reactive extensions is the foundation of Asynchronization, in addition, in some advanced applications, syntax Sugar cannot replace it. It is necessary to familiarize yourself with this framework.

At present, I am still familiar with things, but I still don't know much about it. I will write an article to introduce it later. If you are interested in this database, you can visit the following links in the repository:

Reactive extensions for. Net (RX)

RX: curing your asynchronous programming blues

Introducing the reactive framework

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.