How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

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

    How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

    I wrote a simple program in VB6 to copy all the files from a directory
    on a CD-ROM to my hard disk. There are about 10 files, each about
    30MB.

    The program uses Get and Put to get data from the CD into a buffer and
    then put it into the disk. See code below. It works, but it slows
    down drastically before it copies all the files. Windows Task Manager
    shows the CPU usage gradually increasing as the files are copied,
    until it reaches 100 percent after about 6 files. That's when it
    starts to get slow.

    How can I prevent the CPU usage increasing? Here's the guts of the
    code. All this is within another loop that goes through all the files
    in sCDPath with Dir$. I've tried buffer sizes from 50000 to 2
    million, makes no difference.

    iCDFN = FreeFile
    Open sCDPath & sFileName For Binary Access Read As #iCDFN
    iDiskFN = FreeFile
    Open sDiskPath & sFileName For Binary Access Write As #iDiskFN
    Do
    If lStartPos + lIncrement > lFileLen Then _
    lIncrement = lFileLen - lStartPos + 1
    sBuffer = String(lIncreme nt, " ")
    Get #iCDFN, , sBuffer
    Put #iDiskFN, , sBuffer
    lStartPos = lStartPos + lIncrement
    If lStartPos > lFileLen Then Exit Do
    Loop
    Close iCDFN
    Close iDiskFN

    TIA - Bryan Rickard
  • J French

    #2
    Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

    On 7 Jan 2004 18:06:29 -0800, bwr@unicon.com (Bryan Rickard) wrote:
    [color=blue]
    >I wrote a simple program in VB6 to copy all the files from a directory
    >on a CD-ROM to my hard disk. There are about 10 files, each about
    >30MB.
    >
    >The program uses Get and Put to get data from the CD into a buffer and
    >then put it into the disk. See code below. It works, but it slows
    >down drastically before it copies all the files. Windows Task Manager
    >shows the CPU usage gradually increasing as the files are copied,
    >until it reaches 100 percent after about 6 files. That's when it
    >starts to get slow.[/color]

    <snip>

    What actually gets slow ?

    You are doing a heck of a lot of work
    - you should expect the CPU usage to be high

    If other Apps are responding sluggishly then put a DoEvents after each
    block read/write


    Comment

    • Dikkie Dik

      #3
      Re: How can I prevent CPU Usage increasing to 100 percent when copyingfiles from CD?

      Bryan Rickard wrote:
      [color=blue]
      > I wrote a simple program in VB6 to copy all the files from a directory
      > on a CD-ROM to my hard disk. There are about 10 files, each about
      > 30MB.
      >
      > The program uses Get and Put to get data from the CD into a buffer and
      > then put it into the disk. See code below. It works, but it slows
      > down drastically before it copies all the files. Windows Task Manager
      > shows the CPU usage gradually increasing as the files are copied,
      > until it reaches 100 percent after about 6 files. That's when it
      > starts to get slow.
      >
      > How can I prevent the CPU usage increasing? Here's the guts of the
      > code. All this is within another loop that goes through all the files
      > in sCDPath with Dir$. I've tried buffer sizes from 50000 to 2
      > million, makes no difference.
      >
      > iCDFN = FreeFile
      > Open sCDPath & sFileName For Binary Access Read As #iCDFN
      > iDiskFN = FreeFile
      > Open sDiskPath & sFileName For Binary Access Write As #iDiskFN
      > Do
      > If lStartPos + lIncrement > lFileLen Then _
      > lIncrement = lFileLen - lStartPos + 1
      > sBuffer = String(lIncreme nt, " ")
      > Get #iCDFN, , sBuffer
      > Put #iDiskFN, , sBuffer
      > lStartPos = lStartPos + lIncrement
      > If lStartPos > lFileLen Then Exit Do
      > Loop
      > Close iCDFN
      > Close iDiskFN
      >
      > TIA - Bryan Rickard[/color]

      You could use an API call. Look at
      Your #1 source for using API-functions in Visual Basic and classes for the .NET runtime!

      For more details

      Best regards,
      Dikkie Dik

      Comment

      • Bryan Rickard

        #4
        Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

        Thank you, Dikkie. Actually I could use FileCopy directly in Visual
        Basic, but I wanted to show the user a ProgressBar showing progress of
        each file transfer. That's why I wanted to break the copying into
        chunks. FileCopy doesn't allow anything else to happen in the
        application until the whole file has been copied. I don't think the
        direct API call would be any different.

        - Bryan

        Dikkie Dik <Abuse@SpamBust ers.com> wrote in message news:<3ffd3707$ 0$22323$7a628dc 9@dreader-2.news.scarlet-internet.nl>...[color=blue]
        > Bryan Rickard wrote:
        >[color=green]
        > > I wrote a simple program in VB6 to copy all the files from a directory
        > > on a CD-ROM to my hard disk. There are about 10 files, each about
        > > 30MB.
        > >
        > > The program uses Get and Put to get data from the CD into a buffer and
        > > then put it into the disk. See code below. It works, but it slows
        > > down drastically before it copies all the files. Windows Task Manager
        > > shows the CPU usage gradually increasing as the files are copied,
        > > until it reaches 100 percent after about 6 files. That's when it
        > > starts to get slow.
        > >
        > > How can I prevent the CPU usage increasing? Here's the guts of the
        > > code. All this is within another loop that goes through all the files
        > > in sCDPath with Dir$. I've tried buffer sizes from 50000 to 2
        > > million, makes no difference.
        > >
        > > iCDFN = FreeFile
        > > Open sCDPath & sFileName For Binary Access Read As #iCDFN
        > > iDiskFN = FreeFile
        > > Open sDiskPath & sFileName For Binary Access Write As #iDiskFN
        > > Do
        > > If lStartPos + lIncrement > lFileLen Then _
        > > lIncrement = lFileLen - lStartPos + 1
        > > sBuffer = String(lIncreme nt, " ")
        > > Get #iCDFN, , sBuffer
        > > Put #iDiskFN, , sBuffer
        > > lStartPos = lStartPos + lIncrement
        > > If lStartPos > lFileLen Then Exit Do
        > > Loop
        > > Close iCDFN
        > > Close iDiskFN
        > >
        > > TIA - Bryan Rickard[/color]
        >
        > You could use an API call. Look at
        > http://www.mentalis.org/apilist/CopyFile.shtml
        > For more details
        >
        > Best regards,
        > Dikkie Dik[/color]

        Comment

        • Bryan Rickard

          #5
          Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

          The copying gets slow. See following log extract. After the sixth
          file it starts to slow and gets worse.

          c9301.z: size = 34,149,859 bytes, time = 22 seconds, speed = 1.552
          Mb/sec.
          c9302.z: size = 33,209,029 bytes, time = 18 seconds, speed = 1.845
          Mb/sec.
          c9303.z: size = 33,427,953 bytes, time = 17 seconds, speed = 1.966
          Mb/sec.
          c9304.z: size = 33,270,682 bytes, time = 16 seconds, speed = 2.079
          Mb/sec.
          c9305.z: size = 32,773,719 bytes, time = 15 seconds, speed = 2.185
          Mb/sec.
          c9306.z: size = 32,319,428 bytes, time = 14 seconds, speed = 2.309
          Mb/sec.
          c9307.z: size = 31,733,326 bytes, time = 31 seconds, speed = 1.024
          Mb/sec.
          c9308.z: size = 31,580,633 bytes, time = 51 seconds, speed = 0.619
          Mb/sec.
          c9309.z: size = 31,101,258 bytes, time = 71 seconds, speed = 0.438
          Mb/sec.
          l93.z: size = 75,610,329 bytes, time = 340 seconds, speed = 0.222
          Mb/sec.

          There is a DoEvents after each block read/write.

          - Bryan


          erewhon@nowhere .com (J French) wrote in message news:<3ffd294b. 93242285@news.b tclick.com>...[color=blue]
          > On 7 Jan 2004 18:06:29 -0800, bwr@unicon.com (Bryan Rickard) wrote:
          >[color=green]
          > >I wrote a simple program in VB6 to copy all the files from a directory
          > >on a CD-ROM to my hard disk. There are about 10 files, each about
          > >30MB.
          > >
          > >The program uses Get and Put to get data from the CD into a buffer and
          > >then put it into the disk. See code below. It works, but it slows
          > >down drastically before it copies all the files. Windows Task Manager
          > >shows the CPU usage gradually increasing as the files are copied,
          > >until it reaches 100 percent after about 6 files. That's when it
          > >starts to get slow.[/color]
          >
          > <snip>
          >
          > What actually gets slow ?
          >
          > You are doing a heck of a lot of work
          > - you should expect the CPU usage to be high
          >
          > If other Apps are responding sluggishly then put a DoEvents after each
          > block read/write[/color]

          Comment

          • Steve Gerrard

            #6
            Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

            See Randy Birch's stuff for a way to use APIs to copy files with
            progress reports (and cancel ability):

            VBnet provides Intermediate and Advanced Win32 API code for VB developers. Comprehensive Code, FAQ, Developers Resources & News, alphabetical API/Type/Constant/Method Index, along with the largest Visual Basic-related links list on the net.



            "Bryan Rickard" <bwr@unicon.com > wrote in message
            news:2b02b188.0 401081409.5ce12 daa@posting.goo gle.com...[color=blue]
            > Thank you, Dikkie. Actually I could use FileCopy directly in Visual
            > Basic, but I wanted to show the user a ProgressBar showing progress of
            > each file transfer. That's why I wanted to break the copying into
            > chunks. FileCopy doesn't allow anything else to happen in the
            > application until the whole file has been copied. I don't think the
            > direct API call would be any different.
            >
            > - Bryan
            >
            > Dikkie Dik <Abuse@SpamBust ers.com> wrote in message[/color]
            news:<3ffd3707$ 0$22323$7a628dc 9@dreader-2.news.scarlet-internet.nl>...[color=blue][color=green]
            > > Bryan Rickard wrote:
            > >[color=darkred]
            > > > I wrote a simple program in VB6 to copy all the files from a[/color][/color][/color]
            directory[color=blue][color=green][color=darkred]
            > > > on a CD-ROM to my hard disk. There are about 10 files, each about
            > > > 30MB.
            > > >
            > > > The program uses Get and Put to get data from the CD into a buffer[/color][/color][/color]
            and[color=blue][color=green][color=darkred]
            > > > then put it into the disk. See code below. It works, but it[/color][/color][/color]
            slows[color=blue][color=green][color=darkred]
            > > > down drastically before it copies all the files. Windows Task[/color][/color][/color]
            Manager[color=blue][color=green][color=darkred]
            > > > shows the CPU usage gradually increasing as the files are copied,
            > > > until it reaches 100 percent after about 6 files. That's when it
            > > > starts to get slow.
            > > >
            > > > How can I prevent the CPU usage increasing? Here's the guts of[/color][/color][/color]
            the[color=blue][color=green][color=darkred]
            > > > code. All this is within another loop that goes through all the[/color][/color][/color]
            files[color=blue][color=green][color=darkred]
            > > > in sCDPath with Dir$. I've tried buffer sizes from 50000 to 2
            > > > million, makes no difference.
            > > >
            > > > iCDFN = FreeFile
            > > > Open sCDPath & sFileName For Binary Access Read As #iCDFN
            > > > iDiskFN = FreeFile
            > > > Open sDiskPath & sFileName For Binary Access Write As #iDiskFN
            > > > Do
            > > > If lStartPos + lIncrement > lFileLen Then _
            > > > lIncrement = lFileLen - lStartPos + 1
            > > > sBuffer = String(lIncreme nt, " ")
            > > > Get #iCDFN, , sBuffer
            > > > Put #iDiskFN, , sBuffer
            > > > lStartPos = lStartPos + lIncrement
            > > > If lStartPos > lFileLen Then Exit Do
            > > > Loop
            > > > Close iCDFN
            > > > Close iDiskFN
            > > >
            > > > TIA - Bryan Rickard[/color]
            > >
            > > You could use an API call. Look at
            > > http://www.mentalis.org/apilist/CopyFile.shtml
            > > For more details
            > >
            > > Best regards,
            > > Dikkie Dik[/color][/color]


            Comment

            • Steve Gerrard

              #7
              Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?


              "Bryan Rickard" <bwr@unicon.com > wrote in message
              news:2b02b188.0 401071806.1337d 16d@posting.goo gle.com...[color=blue]
              > I wrote a simple program in VB6 to copy all the files from a directory
              > on a CD-ROM to my hard disk. There are about 10 files, each about
              > 30MB.
              >
              > The program uses Get and Put to get data from the CD into a buffer and
              > then put it into the disk. See code below. It works, but it slows
              > down drastically before it copies all the files. Windows Task Manager
              > shows the CPU usage gradually increasing as the files are copied,
              > until it reaches 100 percent after about 6 files. That's when it
              > starts to get slow.
              >
              > How can I prevent the CPU usage increasing? Here's the guts of the
              > code. All this is within another loop that goes through all the files
              > in sCDPath with Dir$. I've tried buffer sizes from 50000 to 2
              > million, makes no difference.
              >
              > iCDFN = FreeFile
              > Open sCDPath & sFileName For Binary Access Read As #iCDFN
              > iDiskFN = FreeFile
              > Open sDiskPath & sFileName For Binary Access Write As #iDiskFN
              > Do
              > If lStartPos + lIncrement > lFileLen Then _
              > lIncrement = lFileLen - lStartPos + 1
              > sBuffer = String(lIncreme nt, " ")
              > Get #iCDFN, , sBuffer
              > Put #iDiskFN, , sBuffer
              > lStartPos = lStartPos + lIncrement
              > If lStartPos > lFileLen Then Exit Do
              > Loop
              > Close iCDFN
              > Close iDiskFN
              >
              > TIA - Bryan Rickard[/color]

              You are reallocating sBuffer each time through the loop. If you
              calculate the leftover first, you can copy that amount, then copy the
              rest of the file using the same block.

              ' Get the length of the source file.
              lFileLen = LOF(nSource)

              ' Calculate the left over.
              nLeftOver = lFileLen Mod lIncrement

              ' Create a buffer for the nLeftOver amount.
              sBuffer= String$(nLeftOv er, " ")

              ' Read and write the nLeftOver amount.
              Get #iCDFN, , strBuffer
              Put #iDiskFN, , strBuffer
              lStartPos = nLeftOver

              ' Create a buffer for a block (do only once)
              sBuffer = String$(lIncrem ent, " ")

              ' Read and write the remaining blocks of data.
              Do Until lStartPos > lFileLen
              ' Read and write one block of data.
              Get #iCDFN, , strBuffer
              Put #iDiskFN, , strBuffer
              lStartPos = lStartPos + lIncrement
              Loop


              Comment

              • J French

                #8
                Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

                On Thu, 8 Jan 2004 15:54:49 -0800, "Steve Gerrard"
                <notstevegerrar d@comcast.net> wrote:

                <snip>

                [color=blue]
                >You are reallocating sBuffer each time through the loop. If you
                >calculate the leftover first, you can copy that amount, then copy the
                >rest of the file using the same block.[/color]

                Spot on - memory reallocation !

                It might be an idea to use Byte Arrays

                I am not sure about copying the 'residue' first
                - it is an interesting idea
                - but theoretically (if not in practice) it removes the possibility of
                alignment of clusters/sectors

                It just makes me feel uncomfortable ...

                Comment

                • Steve Gerrard

                  #9
                  Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?


                  "J French" <erewhon@nowher e.com> wrote in message
                  news:3ffe9a50.9 0065265@news.bt click.com...[color=blue]
                  > On Thu, 8 Jan 2004 15:54:49 -0800, "Steve Gerrard"
                  > <notstevegerrar d@comcast.net> wrote:
                  >
                  >[color=green]
                  > >You are reallocating sBuffer each time through the loop. If you
                  > >calculate the leftover first, you can copy that amount, then copy the
                  > >rest of the file using the same block.[/color]
                  >
                  > Spot on - memory reallocation !
                  >
                  > It might be an idea to use Byte Arrays
                  >
                  > I am not sure about copying the 'residue' first
                  > - it is an interesting idea
                  > - but theoretically (if not in practice) it removes the possibility of
                  > alignment of clusters/sectors
                  >
                  > It just makes me feel uncomfortable ...[/color]

                  I agree, it does seem odd. I actually got the basis of that code from a
                  MS article on file copying in networks, and that is how they did it. (It
                  was an old article addressing a collision problem in older 10 Mbps
                  networks.)

                  I suppose if you calculate the number of "std" blocks as well, you could
                  do all of them first, then do the residue block, or something like that.


                  Comment

                  • Bob Butler

                    #10
                    Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

                    "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message news:<bK6dnd8B0 fhecGCiRVn-ug@comcast.com> ...
                    <cut>

                    I've done this sort of thing often but never thought to put the
                    "leftover" chunk first like that... I'll have to try it. One minor
                    change:
                    [color=blue]
                    >
                    > ' Get the length of the source file.
                    > lFileLen = LOF(nSource)
                    >
                    > ' Calculate the left over.
                    > nLeftOver = lFileLen Mod lIncrement[/color]

                    If nLeftOver>0 Then
                    [color=blue]
                    > ' Create a buffer for the nLeftOver amount.
                    > sBuffer= String$(nLeftOv er, " ")
                    >
                    > ' Read and write the nLeftOver amount.
                    > Get #iCDFN, , strBuffer
                    > Put #iDiskFN, , strBuffer
                    > lStartPos = nLeftOver[/color]

                    End If
                    [color=blue]
                    > ' Create a buffer for a block (do only once)
                    > sBuffer = String$(lIncrem ent, " ")
                    >
                    > ' Read and write the remaining blocks of data.
                    > Do Until lStartPos > lFileLen
                    > ' Read and write one block of data.
                    > Get #iCDFN, , strBuffer
                    > Put #iDiskFN, , strBuffer
                    > lStartPos = lStartPos + lIncrement
                    > Loop[/color]

                    Comment

                    • Bryan Rickard

                      #11
                      Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

                      Steve, thank you very much! That took care of the problem.

                      I wonder, though, why VB doesn't reallocate the same memory when you
                      redefine a fixed-length string. You would think it would.

                      - Bryan

                      "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message news:<bK6dnd8B0 fhecGCiRVn-ug@comcast.com> ...[color=blue]
                      > "Bryan Rickard" <bwr@unicon.com > wrote in message
                      > news:2b02b188.0 401071806.1337d 16d@posting.goo gle.com...[color=green]
                      > > I wrote a simple program in VB6 to copy all the files from a directory
                      > > on a CD-ROM to my hard disk. There are about 10 files, each about
                      > > 30MB.
                      > >
                      > > The program uses Get and Put to get data from the CD into a buffer and
                      > > then put it into the disk. See code below. It works, but it slows
                      > > down drastically before it copies all the files. Windows Task Manager
                      > > shows the CPU usage gradually increasing as the files are copied,
                      > > until it reaches 100 percent after about 6 files. That's when it
                      > > starts to get slow.
                      > >
                      > > How can I prevent the CPU usage increasing? Here's the guts of the
                      > > code. All this is within another loop that goes through all the files
                      > > in sCDPath with Dir$. I've tried buffer sizes from 50000 to 2
                      > > million, makes no difference.
                      > >
                      > > iCDFN = FreeFile
                      > > Open sCDPath & sFileName For Binary Access Read As #iCDFN
                      > > iDiskFN = FreeFile
                      > > Open sDiskPath & sFileName For Binary Access Write As #iDiskFN
                      > > Do
                      > > If lStartPos + lIncrement > lFileLen Then _
                      > > lIncrement = lFileLen - lStartPos + 1
                      > > sBuffer = String(lIncreme nt, " ")
                      > > Get #iCDFN, , sBuffer
                      > > Put #iDiskFN, , sBuffer
                      > > lStartPos = lStartPos + lIncrement
                      > > If lStartPos > lFileLen Then Exit Do
                      > > Loop
                      > > Close iCDFN
                      > > Close iDiskFN
                      > >
                      > > TIA - Bryan Rickard[/color]
                      >
                      > You are reallocating sBuffer each time through the loop. If you
                      > calculate the leftover first, you can copy that amount, then copy the
                      > rest of the file using the same block.
                      >
                      > ' Get the length of the source file.
                      > lFileLen = LOF(nSource)
                      >
                      > ' Calculate the left over.
                      > nLeftOver = lFileLen Mod lIncrement
                      >
                      > ' Create a buffer for the nLeftOver amount.
                      > sBuffer= String$(nLeftOv er, " ")
                      >
                      > ' Read and write the nLeftOver amount.
                      > Get #iCDFN, , strBuffer
                      > Put #iDiskFN, , strBuffer
                      > lStartPos = nLeftOver
                      >
                      > ' Create a buffer for a block (do only once)
                      > sBuffer = String$(lIncrem ent, " ")
                      >
                      > ' Read and write the remaining blocks of data.
                      > Do Until lStartPos > lFileLen
                      > ' Read and write one block of data.
                      > Get #iCDFN, , strBuffer
                      > Put #iDiskFN, , strBuffer
                      > lStartPos = lStartPos + lIncrement
                      > Loop[/color]

                      Comment

                      • Bryan Rickard

                        #12
                        Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

                        Steve, thank you very much! That took care of the problem.

                        I wonder, though, why VB doesn't reallocate the same memory when you
                        redefine a fixed-length string. You would think it would.


                        "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message news:<bK6dnd8B0 fhecGCiRVn-ug@comcast.com> ...[color=blue]
                        >
                        > You are reallocating sBuffer each time through the loop. If you
                        > calculate the leftover first, you can copy that amount, then copy the
                        > rest of the file using the same block.
                        >
                        > ' Get the length of the source file.
                        > lFileLen = LOF(nSource)
                        >
                        > ' Calculate the left over.
                        > nLeftOver = lFileLen Mod lIncrement
                        >
                        > ' Create a buffer for the nLeftOver amount.
                        > sBuffer= String$(nLeftOv er, " ")
                        >
                        > ' Read and write the nLeftOver amount.
                        > Get #iCDFN, , strBuffer
                        > Put #iDiskFN, , strBuffer
                        > lStartPos = nLeftOver
                        >
                        > ' Create a buffer for a block (do only once)
                        > sBuffer = String$(lIncrem ent, " ")
                        >
                        > ' Read and write the remaining blocks of data.
                        > Do Until lStartPos > lFileLen
                        > ' Read and write one block of data.
                        > Get #iCDFN, , strBuffer
                        > Put #iDiskFN, , strBuffer
                        > lStartPos = lStartPos + lIncrement
                        > Loop[/color]

                        Comment

                        • Steve Gerrard

                          #13
                          Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?


                          "Bryan Rickard" <bwr@unicon.com > wrote in message
                          news:2b02b188.0 401091552.ecec8 35@posting.goog le.com...[color=blue]
                          > Steve, thank you very much! That took care of the problem.
                          >
                          > I wonder, though, why VB doesn't reallocate the same memory when you
                          > redefine a fixed-length string. You would think it would.
                          >
                          > - Bryan
                          >
                          > "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message[/color]
                          news:<bK6dnd8B0 fhecGCiRVn-ug@comcast.com> ...[color=blue][color=green]
                          > > You are reallocating sBuffer each time through the loop. If you
                          > > calculate the leftover first, you can copy that amount, then copy[/color][/color]
                          the[color=blue][color=green]
                          > > rest of the file using the same block.
                          > >[/color][/color]

                          Actually I'm somewhat suprised it makes such a difference. I can see
                          that the new block has to be allocated before the old block is released,
                          but you would think that about the third or fourth time it would be able
                          to reuse the first block. Maybe something else smaller gets allocated in
                          the mean time, so the free memory gets "fragmented " like a disk, and the
                          new block has to keep being allocated "at the top".


                          Comment

                          • Steve Gerrard

                            #14
                            Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?


                            "Bob Butler" <butlerbob@eart hlink.net> wrote in message
                            news:fa10fb0.04 01091255.3487cb d3@posting.goog le.com...[color=blue]
                            > "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message[/color]
                            news:<bK6dnd8B0 fhecGCiRVn-ug@comcast.com> ...[color=blue]
                            > <cut>
                            >
                            > I've done this sort of thing often but never thought to put the
                            > "leftover" chunk first like that... I'll have to try it. One minor
                            > change:
                            >[color=green]
                            > >
                            > > ' Get the length of the source file.
                            > > lFileLen = LOF(nSource)
                            > >
                            > > ' Calculate the left over.
                            > > nLeftOver = lFileLen Mod lIncrement[/color]
                            >
                            > If nLeftOver>0 Then
                            >[color=green]
                            > > ' Create a buffer for the nLeftOver amount.
                            > > sBuffer= String$(nLeftOv er, " ")
                            > >
                            > > ' Read and write the nLeftOver amount.
                            > > Get #iCDFN, , strBuffer
                            > > Put #iDiskFN, , strBuffer
                            > > lStartPos = nLeftOver[/color]
                            >
                            > End If
                            >[color=green]
                            > > ' Create a buffer for a block (do only once)
                            > > sBuffer = String$(lIncrem ent, " ")
                            > >
                            > > ' Read and write the remaining blocks of data.
                            > > Do Until lStartPos > lFileLen
                            > > ' Read and write one block of data.
                            > > Get #iCDFN, , strBuffer
                            > > Put #iDiskFN, , strBuffer
                            > > lStartPos = lStartPos + lIncrement
                            > > Loop[/color][/color]

                            Even though this has been working for me, I liked J. French's point that
                            there is something odd about writing the leftover block first. I think
                            you could calc the size, run all the other blocks, and write the
                            leftover at the end just as easily, which somehow sounds better, sincee
                            you probably made your block size a multiple of 512 to match the sector
                            size.

                            My code without the check for nLeftOver = 0 has been working fine for
                            quite a while, so I think the zero length get and put must be harmless.
                            However, it does seem like better programming to check for it.


                            Comment

                            • J French

                              #15
                              Re: How can I prevent CPU Usage increasing to 100 percent when copying files from CD?

                              On Fri, 9 Jan 2004 11:41:22 -0800, "Steve Gerrard"
                              <notstevegerrar d@comcast.net> wrote:

                              <snip>
                              [color=blue][color=green]
                              >>
                              >> It just makes me feel uncomfortable ...[/color]
                              >
                              >I agree, it does seem odd. I actually got the basis of that code from a
                              >MS article on file copying in networks, and that is how they did it. (It
                              >was an old article addressing a collision problem in older 10 Mbps
                              >networks.)
                              >
                              >I suppose if you calculate the number of "std" blocks as well, you could
                              >do all of them first, then do the residue block, or something like that.[/color]

                              That is pretty much how I do it
                              - but I use a While/Wend

                              While BytesDone < TotalFileLen

                              alternatively :-
                              While BytesToCopy > 0

                              There is also something to be said for 'pre-extending' the destination
                              file to its final length before actually copying any data
                              - it saves the OS faffing about

                              Comment

                              Working...