In the past, I used BinaryReader to read several bytes, but lately I got this error:
An error has occurred: Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader or TextWriter Synchronized methods. This also applies to classes like StreamWriter and StreamReader. at System.Buffer.InternalBlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count) at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count) at System.IO.BinaryReader.FillBuffer(Int32 numBytes) at System.IO.BinaryReader.ReadUInt16()
So, as a result, I decided to use synchronous TextReader methods, such as:
public class SafeReader { private Stream m_Stream; private TextReader m_TextReader; public SafeReader(Stream stream) { m_TextReader = TextReader.Synchronized(new StreamReader(m_Stream = stream)); } public Stream BaseStream { get { return m_Stream; } } public int ReadInt32() {
However, the return values ββ(pretty much reads the image in native format) are incorrect. "Images" have a slight bluish tint, and I feel that this may be due to the fact that TextReader reads text (and reads encoded characters, rather than just reading byte values).
Is there a "thread safe" way, for example TextReader Synchronized () for reading binary files?
source share