binarywriter.write loop is causing 100% cpu

S

showson1

Hi all!
I have some files that are basically a TIF with an ASCII header.
I wrote an app that reads in the file, pulls some values from the
header and writes out everything after the header as a new file.

I'm using binaryreader and binarywriter.
Everything functions fine, but the following loop causes 100% CPU
usage and I can't figure out how to get around this.

Here is a code sample that lists the declarations and the loop that's
causing the high CPU usage:
----------------------

Dim fsIn As FileStream = New FileStream(filename,
FileMode.OpenOrCreate)
Dim fsOut As FileStream = New FileStream(fsIn.Name & ".tif",
FileMode.Create)
Dim binWriter As BinaryWriter
Dim binReader As BinaryReader = New BinaryReader(fsIn)

binWriter = New BinaryWriter(fsOut)

Do While binReader.BaseStream.Position <
binReader.BaseStream.Length
binWriter.Write(binReader.ReadByte)
Loop

binWriter.Flush()
binWriter.Close()
binReader.Close()
binReader = Nothing
fsIn.Close()
fsIn = Nothing
fsOut.Close()
fsOut = Nothing
----------------------

Obviously I left some stuff out, but that's the part that's using the
CPU.
The binReader.BaseStream.Position picks up where the loop to find the
end of the header left off.

If anyone has any suggestions or tips as to what's going on and / or
how I can get around it I'd really appreciate it!
Thanks!
 
G

Guest

In your loop you are reading and writing one byte at a time. I would read
into and write from a byte array using readbytes(bArray). Your loop would
then just continue until the bytes read (returned by readbytes) is less than
the number of bytes expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top