public void fileReader()
{
Stream stream = new FileStream(filename, FileMode.Open , FileAccess.Read , FileShare.None, 30, true);
byte[] Buffer = new byte[30];
while (stream.Read(Buffer, 0, 30) > 0)
{
label1.text=Encoding.UTF8.GetString(Buffer);
Thread.Sleep(1000);
}
stream.Dispose();
}
THIS IS MY C# CODE TO READ 30 BYTES OF DATA AT A TIME FROM A FILE. I’ve created the Stream with FileStream constructor having useAsync=true. Here stream.Read method is used.
-
Is this read operation working in ASYNCHRNOUS mode?
-
How to check this?
-
Another problem is that
Encoding.UTF8.GetString(Buffer);gives white spaces as ‘□’ character… y? -
Is there any other way to make a 1 second delay other than
Thread.Sleep(1000);?
It doesn’t since you are using the syncronous
Readmethodif you wish to read it asyncronous you would have to use the
BeginReadmethodBelow is an example on the usage of BeginRead from MSDN
That said you should probably use a StreamReader to read the characters of the stream instead of converting them yourself.
if you are using .NET 4.5 you can use ReadAsync as below (again from
[MSDN][2])private async void Button_Click(object sender, RoutedEventArgs e)
{
UnicodeEncoding uniencoding = new UnicodeEncoding();
string filename = @”c:\Users\exampleuser\Documents\userinputlog.txt”;
byte[] result;