Sure. Since it's pretty compact, here it is:
// aioee.cs - Testing Asynchronous I/O - EE ask version
using System;
using System.IO;
using System.Text;
using System.Threading;
namespace AIOEE
{
public class AsyncTest
{
// private const int bufferSize = 10; // to try to slow it down
private const int bufferSize = 256; // arbitrary
// private const int bufferSize = 4000; // suggested by djon2003
// private const int bufferSize = 1000000000; // fast, of course
private const string inputFileName = "somefile.txt";
private byte[] buffer;
private Stream inStream;
private AsyncCallback callbackMethod;
public AsyncTest()
{
inStream = File.OpenRead(inputFileName);
buffer = new byte[bufferSize];
callbackMethod = new AsyncCallback(this.OnCompletedRead);
} // end AsyncTest() ctor
public static void Main(string[] args)
{
AsyncTest tester = new AsyncTest();
tester.Run();
} // end Main()
public void Run(string[] args)
{
// using null instead of a local state object
inStream.BeginRead(buffer, 0, buffer.Length, callbackMethod, null);
Console.WriteLine("Run(): Immediately following BeginRead() call.");
// do some token work in between to prove that we're asynchronous.
// Note that this never shows up until the end regardless of file/buffer sizes,
// which means the BeginRead() is blocking.
for (long i=0; i<=100000; i++)
{
if (i%1000 == 0)
{
Console.WriteLine("i: {0}", i);
} // end if
} // end for
} // end Run()
// Callback method.
void OnCompletedRead(IAsyncResult asyncResult)
{
Console.WriteLine("HIT ONCOMPLETEDREAD Method.");
int bytesRead = inStream.EndRead(asyncResult);
if (bytesRead == 0)
{
Console.WriteLine("OnCompletedRead(): No bytes read -- must be done");
}
else
{
string s = Encoding.ASCII.GetString(buffer, 0, bytesRead);
// my this could get large
// Console.WriteLine(s);
// If bytes have been read, try to read more
// Is this why I'm blocking?
// Is this also why I'm bombing on large files with small buffer sizes?
inStream.BeginRead(buffer, 0, buffer.Length, callbackMethod, null);
} // end if-else
} // end OnCompletedRead()
} // end class AsyncTest
} // end namespace AIOEE
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87:





by: djon2003Posted on 2009-04-19 at 12:53:12ID: 24180329
OK.. First.. where to you put the line EndRead ? It should be in your callback method. Isn't it ? If so, you should set a buffer around 4000, which tells the reader to read 4000 bytes at a time.
So, if you have a file of 40000 bytes, your callback method will be called with a 4000 bytes array (the first ones). The callback method shall have the EndRead as first call into the method. Then looking if the buffer return is not empty, you call again the BeginRead, or if it is, you exit the method.
So, you will have to combines to bytes into a Collection, and when finished, you will have to whole file read. This should gives you an Asynchronous way to read the file. Although, I never read a file asynchronously, I just use this with NetworkStream, it should work. If not, give me the code where all BeginRead and EndRead are located. (The whole methods).