Tuesday, July 25, 2006

New .Net 2.0 Stream.ReadTimout and Stream.WriteTimout

The new .Net 2.0 Stream.ReadTimout and Stream.WriteTimout properties don't work with all derived classes, such as FileStream. Therefore, always ensure that you check the Stream.CanTimeout property before calling any of these new properties. using (FileStream fs = new FileStream( @"C:\boot.ini", FileMode.Open, FileAccess.Read)) { if (fs.CanTimeout) { fs.ReadTimeout = 1; // 1 millisecond fs.WriteTimeout = 1; // 1 millisecond } int intByte = fs.ReadByte(); while (intByte > -1) { this.txtStatus.Text += Encoding.ASCII.GetString(new byte[] {(byte)intByte}); intByte = fs.ReadByte(); } }

No comments: