Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Weird delay when writing a file

Weird delay when writing a file

Scheduled Pinned Locked Moved C#
helpdotnetcomsysadmindebugging
24 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    svanwass
    wrote on last edited by
    #1

    I am getting a weird delay when writing a file. I am not able to figure out where the delay is coming from. The facts: I map a drive to a NT server (W:\) I copy a file at W:\largefile.dat to W:\Archive\largefile.dat My copy loop:

    try
    {
    using (FileStream sourceStream = new FileStream(nd_CopyDrive.LocalDrive + "\\" + fi.Name, FileMode.Open))
    {
    byte[] buffer = new byte[64 * 1024];
    using (FileStream destStream = new FileStream(nd_CopyDrive.LocalDrive + "\\Archive\\" + fi.Name, FileMode.Create))
    {
    int q;
    while ((q = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
    {
    destStream.Write(buffer, 0, q);
    }
    }
    }
    }

    When the file is completely written my program hangs at the closing bracket for the second using statement. Nothing is shown in the Output window that gives me some clues. This block of code IS running in a background worker as added information. Could my problem be related to how the CLR "releases the memory used to store objects that are no longer required". If I place a debug marker at that ending bracket and perform a step into, the Output window shows:

    Step into: Stepping over method without symbols 'System.IO.Stream.Dispose' (which takes forever to complete)

    Any help would be greatly appreciated!

    Richard Andrew x64R N N G 4 Replies Last reply
    0
    • S svanwass

      I am getting a weird delay when writing a file. I am not able to figure out where the delay is coming from. The facts: I map a drive to a NT server (W:\) I copy a file at W:\largefile.dat to W:\Archive\largefile.dat My copy loop:

      try
      {
      using (FileStream sourceStream = new FileStream(nd_CopyDrive.LocalDrive + "\\" + fi.Name, FileMode.Open))
      {
      byte[] buffer = new byte[64 * 1024];
      using (FileStream destStream = new FileStream(nd_CopyDrive.LocalDrive + "\\Archive\\" + fi.Name, FileMode.Create))
      {
      int q;
      while ((q = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
      {
      destStream.Write(buffer, 0, q);
      }
      }
      }
      }

      When the file is completely written my program hangs at the closing bracket for the second using statement. Nothing is shown in the Output window that gives me some clues. This block of code IS running in a background worker as added information. Could my problem be related to how the CLR "releases the memory used to store objects that are no longer required". If I place a debug marker at that ending bracket and perform a step into, the Output window shows:

      Step into: Stepping over method without symbols 'System.IO.Stream.Dispose' (which takes forever to complete)

      Any help would be greatly appreciated!

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      I'll bet if you put a call to destStream.Flush() just after the Write while loop, it would solve the mystery. I'm reasonably sure that the delay is due to the data actually being written to the file.

      N 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        I'll bet if you put a call to destStream.Flush() just after the Write while loop, it would solve the mystery. I'm reasonably sure that the delay is due to the data actually being written to the file.

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        Flush is called by Close which is called by Dispose, which, of course, is called when leaving the using scope. So no, calling Flush before the won't actually do anything different.


        I know the language. I've read a book. - _Madmatt

        Richard Andrew x64R 1 Reply Last reply
        0
        • N Not Active

          Flush is called by Close which is called by Dispose, which, of course, is called when leaving the using scope. So no, calling Flush before the won't actually do anything different.


          I know the language. I've read a book. - _Madmatt

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          It would solve the mystery by exposing exactly what's causing the slowdown.

          N 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            It would solve the mystery by exposing exactly what's causing the slowdown.

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            Will it give information about network latency, data packets, etc.? No, it won't expose anything more than an extra statement to set a break point on.


            I know the language. I've read a book. - _Madmatt

            Richard Andrew x64R 1 Reply Last reply
            0
            • S svanwass

              I am getting a weird delay when writing a file. I am not able to figure out where the delay is coming from. The facts: I map a drive to a NT server (W:\) I copy a file at W:\largefile.dat to W:\Archive\largefile.dat My copy loop:

              try
              {
              using (FileStream sourceStream = new FileStream(nd_CopyDrive.LocalDrive + "\\" + fi.Name, FileMode.Open))
              {
              byte[] buffer = new byte[64 * 1024];
              using (FileStream destStream = new FileStream(nd_CopyDrive.LocalDrive + "\\Archive\\" + fi.Name, FileMode.Create))
              {
              int q;
              while ((q = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
              {
              destStream.Write(buffer, 0, q);
              }
              }
              }
              }

              When the file is completely written my program hangs at the closing bracket for the second using statement. Nothing is shown in the Output window that gives me some clues. This block of code IS running in a background worker as added information. Could my problem be related to how the CLR "releases the memory used to store objects that are no longer required". If I place a debug marker at that ending bracket and perform a step into, the Output window shows:

              Step into: Stepping over method without symbols 'System.IO.Stream.Dispose' (which takes forever to complete)

              Any help would be greatly appreciated!

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Have you trying just using File.Copy to see if there is any difference? It does basically the same thing but it may be something to test. Otherwise I'd say it's just the process of finalizing the writing of a large file, nothing you're going to solve without stepping into the unmanaged realm.


              I know the language. I've read a book. - _Madmatt

              S 1 Reply Last reply
              0
              • N Not Active

                Will it give information about network latency, data packets, etc.? No, it won't expose anything more than an extra statement to set a break point on.


                I know the language. I've read a book. - _Madmatt

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #7

                When you step over it, it will take a long time to execute the Flush statement. It would solve the mystery for me, maybe someone else would be too dense.

                N 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  When you step over it, it will take a long time to execute the Flush statement. It would solve the mystery for me, maybe someone else would be too dense.

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #8

                  Richard Andrew x64 wrote:

                  maybe someone else would be too dense.

                  An uncalled for statement. If you can't remain civil then please don't comment. There are other ways to debug a problem than just hitting F10. Knowing and understanding the processing that are occurring is also a big help.


                  I know the language. I've read a book. - _Madmatt

                  Richard Andrew x64R 1 Reply Last reply
                  0
                  • N Not Active

                    Richard Andrew x64 wrote:

                    maybe someone else would be too dense.

                    An uncalled for statement. If you can't remain civil then please don't comment. There are other ways to debug a problem than just hitting F10. Knowing and understanding the processing that are occurring is also a big help.


                    I know the language. I've read a book. - _Madmatt

                    Richard Andrew x64R Offline
                    Richard Andrew x64R Offline
                    Richard Andrew x64
                    wrote on last edited by
                    #9

                    Look who's talking about being civil! You're the guy who's always breaking Rule 3 in HOW TO ANSWER A QUESTION.

                    S 1 Reply Last reply
                    0
                    • N Not Active

                      Have you trying just using File.Copy to see if there is any difference? It does basically the same thing but it may be something to test. Otherwise I'd say it's just the process of finalizing the writing of a large file, nothing you're going to solve without stepping into the unmanaged realm.


                      I know the language. I've read a book. - _Madmatt

                      S Offline
                      S Offline
                      svanwass
                      wrote on last edited by
                      #10

                      Yes, my original statemet was

                      File.Copy(nd_CopyDrive.LocalDrive + "\\" + fi.Name, nd_CopyDrive.LocalDrive + "\\Archive\\" + fi.Name, b_overwrite);

                      This had the exact same delay.

                      N S 2 Replies Last reply
                      0
                      • Richard Andrew x64R Richard Andrew x64

                        Look who's talking about being civil! You're the guy who's always breaking Rule 3 in HOW TO ANSWER A QUESTION.

                        S Offline
                        S Offline
                        svanwass
                        wrote on last edited by
                        #11

                        I certainly didn't mean to start a fight over debugging... :)

                        N 1 Reply Last reply
                        0
                        • S svanwass

                          Yes, my original statemet was

                          File.Copy(nd_CopyDrive.LocalDrive + "\\" + fi.Name, nd_CopyDrive.LocalDrive + "\\Archive\\" + fi.Name, b_overwrite);

                          This had the exact same delay.

                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #12

                          Then again, I'd say there is nothing you're going to be able to do about it. Aside from performing the copy operation on the system where the files reside rather than going through a mapped drive.


                          I know the language. I've read a book. - _Madmatt

                          S 1 Reply Last reply
                          0
                          • S svanwass

                            Yes, my original statemet was

                            File.Copy(nd_CopyDrive.LocalDrive + "\\" + fi.Name, nd_CopyDrive.LocalDrive + "\\Archive\\" + fi.Name, b_overwrite);

                            This had the exact same delay.

                            S Offline
                            S Offline
                            svanwass
                            wrote on last edited by
                            #13

                            Oh, and I did add all these statements after the while loop:

                            destStream.Flush();
                            destStream.Close();
                            destStream.Dispose();

                            Close is where it is hanging at. More thoughts?

                            N 1 Reply Last reply
                            0
                            • S svanwass

                              I certainly didn't mean to start a fight over debugging... :)

                              N Offline
                              N Offline
                              Not Active
                              wrote on last edited by
                              #14

                              No fighting. Some people just resort to insults and deflection when their arguments can't be defended professionally.


                              I know the language. I've read a book. - _Madmatt

                              1 Reply Last reply
                              0
                              • S svanwass

                                Oh, and I did add all these statements after the while loop:

                                destStream.Flush();
                                destStream.Close();
                                destStream.Dispose();

                                Close is where it is hanging at. More thoughts?

                                N Offline
                                N Offline
                                Not Active
                                wrote on last edited by
                                #15

                                As I said it's the process of finalizing and closing a large file. The data must be validated and header information written to the file and entries made in the FAT. Of course, performance may also be affected by the performance of your network and any IO operations also being performed on the destination disk.


                                I know the language. I've read a book. - _Madmatt

                                1 Reply Last reply
                                0
                                • N Not Active

                                  Then again, I'd say there is nothing you're going to be able to do about it. Aside from performing the copy operation on the system where the files reside rather than going through a mapped drive.


                                  I know the language. I've read a book. - _Madmatt

                                  S Offline
                                  S Offline
                                  svanwass
                                  wrote on last edited by
                                  #16

                                  Taking what you said

                                  Aside from performing the copy operation on the system where the files reside rather than going through a mapped drive.

                                  How can I do that?

                                  N 1 Reply Last reply
                                  0
                                  • S svanwass

                                    Taking what you said

                                    Aside from performing the copy operation on the system where the files reside rather than going through a mapped drive.

                                    How can I do that?

                                    N Offline
                                    N Offline
                                    Not Active
                                    wrote on last edited by
                                    #17

                                    Create a service on the machine which you can call or somehow trigger. Perhaps a WCF service, remember WCF isn't just for web, it is also used for interprocess communication


                                    I know the language. I've read a book. - _Madmatt

                                    S 1 Reply Last reply
                                    0
                                    • N Not Active

                                      Create a service on the machine which you can call or somehow trigger. Perhaps a WCF service, remember WCF isn't just for web, it is also used for interprocess communication


                                      I know the language. I've read a book. - _Madmatt

                                      S Offline
                                      S Offline
                                      svanwass
                                      wrote on last edited by
                                      #18

                                      That's going to be difficult. There are ~600 devices this has to happen to.

                                      N 1 Reply Last reply
                                      0
                                      • S svanwass

                                        That's going to be difficult. There are ~600 devices this has to happen to.

                                        N Offline
                                        N Offline
                                        Not Active
                                        wrote on last edited by
                                        #19

                                        svanwass wrote:

                                        devices

                                        :confused: I thought you were mapping a drive to a server? Anyway, not much more to be said. Other than decreasing the size of the file, which I'm sure is not an option, there isn't anything more you can do that I'm aware of.


                                        I know the language. I've read a book. - _Madmatt

                                        S 1 Reply Last reply
                                        0
                                        • N Not Active

                                          svanwass wrote:

                                          devices

                                          :confused: I thought you were mapping a drive to a server? Anyway, not much more to be said. Other than decreasing the size of the file, which I'm sure is not an option, there isn't anything more you can do that I'm aware of.


                                          I know the language. I've read a book. - _Madmatt

                                          S Offline
                                          S Offline
                                          svanwass
                                          wrote on last edited by
                                          #20

                                          Yea, I have to map to 600 servers, archive some files (one of which is large) then copy over new version of those files.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups