8.0 Streams (2)

Source: Internet
Author: User

More things about Streams...

I ' m going to talk mainly about the Asynch I/O.

Since It's a bit complicated, I ' ll explain codes more specifically.

In the class, first, we should define some class member variables.

Private Stream InputStream; Private AsyncCallback mycallback; Private byte [] buffer; Const int (a)

Then, a class constructor.

Program () {   = File.openread (@ "D:\USEFORTEST.txt");    New byte [buffersize];    New AsyncCallback (this. Oncompletedread);}

And now, let's go to the Main () function.

 Public Static void Main () {   new program ();    Theapp.run ();}

When we create a new program Theapp, we invoke the constructor of the program class. In this step, we'll open the file that we want to read using the InputStream and create a buffer as The preparation for do we read from the file. And then, invoke the Run () function.

 void   Run () {  Inputstream.beginread (buffer,  0 , Buffer.     Length, Mycallback, null  );  for  (long  i = 0 ; I < 10000 ;  I++ if  (i% 100  = = 0  ) {CONSOLE.W Riteline (  i: {0}   " , I); }     }}                 

At the beginning of the Run (), the InputStream begins-to-read from the file to the buffer we created BEF Ore. However, the program won ' t wait for the stream to finish the reading process and then continue the next statements. That's an inefficient. Instead, it'll start a new thread to go on (in this case, go on counting numbers). When the stream finishes reading, it would CallBack to write the result. At this step, counting'll stop for a while.

Following shows how to realize the writing out behavior. It depends on how we define the mycallback.

void Oncompletedread (IAsyncResult asyncResult) {    int bytesread = Inputstream.endread ( AsyncResult);     if 0 )    {        0, bytesread);        Console.WriteLine (s);         0 NULL );     }}           

Finnally, the whole codes with the console output.

(Just a part of the off put for it ' s too long)

Begin with counting, and when it finishes the first part of reading, it stops counting and begin to show the file.

After writing out of the file, it return to count numbers again.

Actually, this is the end of the test codes. But if I coded, I meet a small problem that confused me. That's, in the above codes, I counted from 0 to 10000, and got the expected result, however, if I use 500000 instead of 1 0000, we can only get the numbers from maybe 202000 to the end without smaller numbers or the file ' s content. Is it just because the test number was too large to show?

-----------------END &to Be continued-----------------

8.0 Streams (2)

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.