8.0 Streams(2)
2015-05-06 21:01
141 查看
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.
Then, a class constructor.
And now, let's go to the Main() function.
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 what we read from the file. And then, invoke the Run() function.
At the beginning of the Run(), the inputStream begins to read from the file to the buffer we created before. However, the program won't wait for the stream to finish the reading process and then continue the next statements. That's an inefficient way. Instead, it will start a new thread to go on(in this case, go on counting numbers). When the stream finishes reading, it will CallBack to write out the result. At this step, counting will stop for a while.
Following shows how to realize the writing out behavior. It depends on how we define the myCallBack.
Finnally, the whole codes with the console output.
(just a part of the out 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 the file, it return to count numbers again.
Actually, this is the end of the test codes.But when I coded, I meet a small problem that confused me. That is, in the above codes, I counted from 0 to 10000, and got the expected result, however, if I use 500000 instead of 10000, 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 is too large to show?
-----------------END & TO BE CONTINUED-----------------
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 BufferSize = 256;
Then, a class constructor.
Program() { inputStream = File.OpenRead(@"D:\USEFORTEST.txt"); buffer = new byte[BufferSize]; myCallBack = new AsyncCallback(this.OnCompletedRead); }
And now, let's go to the Main() function.
public static void Main() { Program theApp = 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 what 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.WriteLine("i: {0}", i); } } }
At the beginning of the Run(), the inputStream begins to read from the file to the buffer we created before. However, the program won't wait for the stream to finish the reading process and then continue the next statements. That's an inefficient way. Instead, it will start a new thread to go on(in this case, go on counting numbers). When the stream finishes reading, it will CallBack to write out the result. At this step, counting will 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 (bytesRead > 0) { String s = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine(s); inputStream.BeginRead(buffer, 0, buffer.Length, myCallBack, null); } }
Finnally, the whole codes with the console output.
(just a part of the out 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 the file, it return to count numbers again.
Actually, this is the end of the test codes.But when I coded, I meet a small problem that confused me. That is, in the above codes, I counted from 0 to 10000, and got the expected result, however, if I use 500000 instead of 10000, 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 is too large to show?
-----------------END & TO BE CONTINUED-----------------
相关文章推荐
- Axure RP 8.0 基础1-10
- MySQL · 8.0版本更新 · 性能优化篇
- ubuntu16.04+Cuda8.0+CuDnn v5+OpenCV3.1.0+Matlab2014+Python+Caffe安装
- 54.[ubuntu]自己编译安装vim 8.0的方法
- 在Ubuntu Server上源码安装OpenERP(odoo) 8.0,并配置wsgi和nginx运行环境
- Xcode8.0 打包ipa注意事项
- "libcudart.so.8.0 cannot open shared object file: No such file or directory"
- Java8 Streams API 学习笔记
- 在Windows中如何安装及配置Tomcat8.0服务器
- xcode7 打开工程错误 This Document requires xcode8.0 or later.
- ubuntu 安装cuda8.0后,分辨率无法正确显示的解决方案
- ubuntu16.04下的tensorflow源码安装,踩坑&填坑记录[ubuntu16.04+GTX960+CUDA8.0+cuDNN5.1.5]
- 深度学习主机环境配置: Ubuntu16.04 + GeForce GTX 1070 + CUDA8.0 + cuDNN5.1 + TensorFlow
- Ubuntu16.04 +cuda8.0+cudnn+caffe+theano+tensorflow配置明细
- Win7x64+cuda8.0+ Microsoft caffe VS2013配置记录
- Ubuntu14.04 64位机上安装cuda8.0+cudnn5.0操作步骤
- 编译安装vim-8.0 (centos)
- Ubuntu16.04+CUDA8.0+caffe配置
- iOS之Xcode 8.0真机调试运行:This ** is running iOS 10.1.1 (14B100), which may not be supported
- 轻松学会源码编译Vim 8.0