/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Async.cs -- Asynchronously reads a stream
//
// Compile this program with the following command line;
// C:>csc Async.cs
//
using System;
using System.IO;
using System.Threading;
namespace nsStreams
{
public class Async
{
static Byte [] data = new Byte[64];
static bool bDone = true;
static public void Main (string [] args)
{
if (args.Length == 0)
{
Console.WriteLine ("Please enter a file name");
return;
}
FileStream istrm;
try
{
istrm = new FileStream (args[0], FileMode.Open,
FileAccess.Read, FileShare.Read,
data.Length, true);
}
catch (FileNotFoundException)
{
Console.WriteLine (args[0] + " was not found");
return;
}
catch (Exception)
{
Console.WriteLine ("Cannot open " + args[0] +
" for reading");
return;
}
AsyncCallback cb = new AsyncCallback (ShowText);
long Length = istrm.Length;
int count = 0;
while (true)
{
if (Length == istrm.Position)
break;
if (bDone)
{
bDone = false;
istrm.BeginRead (data, 0, data.Length, cb, count);
++count;
}
Thread.Sleep (250);
}
istrm.Close ();
}
static public void ShowText (IAsyncResult result)
{
for (int x = 0; x < data.Length; ++x)
{
if (data[x] == 0)
break;
Console.Write ((char) data[x]);
data[x] = 0;
}
bDone = true;
}
}
}