Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / July 2007

Tip: Looking for answers? Try searching our database.

binarywriter.write loop is causing 100% cpu

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
showson1 - 24 Jul 2007 22:19 GMT
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!
ModelBuilder - 25 Jul 2007 17:44 GMT
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.  

> Hi all!
> I have some files that are basically a TIF with an ASCII header.
[quoted text clipped - 41 lines]
> how I can get around it I'd really appreciate it!
> Thanks!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.