binarywriter.write loop is causing 100% cpu

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • showson1

    #1

    binarywriter.write loop is causing 100% cpu

    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(file name,
    FileMode.OpenOr Create)
    Dim fsOut As FileStream = New FileStream(fsIn .Name & ".tif",
    FileMode.Create )
    Dim binWriter As BinaryWriter
    Dim binReader As BinaryReader = New BinaryReader(fs In)

    binWriter = New BinaryWriter(fs Out)

    Do While binReader.BaseS tream.Position <
    binReader.BaseS tream.Length
    binWriter.Write (binReader.Read Byte)
    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.BaseS tream.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!

  • =?Utf-8?B?TW9kZWxCdWlsZGVy?=

    #2
    RE: binarywriter.wr ite loop is causing 100% cpu

    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(bArra y). Your loop would
    then just continue until the bytes read (returned by readbytes) is less than
    the number of bytes expected.

    "showson1" wrote:
    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(file name,
    FileMode.OpenOr Create)
    Dim fsOut As FileStream = New FileStream(fsIn .Name & ".tif",
    FileMode.Create )
    Dim binWriter As BinaryWriter
    Dim binReader As BinaryReader = New BinaryReader(fs In)
    >
    binWriter = New BinaryWriter(fs Out)
    >
    Do While binReader.BaseS tream.Position <
    binReader.BaseS tream.Length
    binWriter.Write (binReader.Read Byte)
    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.BaseS tream.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!
    >
    >

    Comment

    Working...