I ran into similar problem. Here is the snippet of problem code.
static void Main(string[] args)
{
FileStream stream = new FileStream("test.txt",FileMode.Open);
byte[] data = new byte[2];
var result = stream.BeginRead(data, 0, 2, ReadSuccess, data);
stream.Close();
Console.ReadLine();
}
static void ReadSuccess(IAsyncResult result)
{
Console.WriteLine("Result {0}",(result.AsyncState as byte[])[0]);
}
I had AB in the file, and was expecting 65 in the console.Writeline.
It turns out that stream is closed immediately after I called BeginRead. Simple arrangement is to call Console.ReadLine before I close the stream
static void Main(string[] args)
{
FileStream stream = new FileStream("test.txt",FileMode.Open);
byte[] data = new byte[2];
var result = stream.BeginRead(data, 0, 2, ReadSuccess, data);
Console.ReadLine();
stream.Close();
}
Even in your case BeginRead moved past just like in this case.