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 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
    NavnathKale
    wrote on last edited by
    #21

    try setting buffer length to less as possible but moderate enough. I usually use 512KB or 1MB buffer length for large file handling. Hope this will help you :)

    S 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!

      G Offline
      G Offline
      Gonzoox
      wrote on last edited by
      #22

      Because the size of your file there's not much that can be done, but have you tried BeginWrite and EndWrite??? prolly asynchronous write can help...

      I want to die like my grandfather- asleep, not like the passengers in his car, screaming!

      S 1 Reply Last reply
      0
      • G Gonzoox

        Because the size of your file there's not much that can be done, but have you tried BeginWrite and EndWrite??? prolly asynchronous write can help...

        I want to die like my grandfather- asleep, not like the passengers in his car, screaming!

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

        No, I have not tried Begin/EndWrite. I read the MSDN article but to be sure, testing these out would satisfy Async writing?

        1 Reply Last reply
        0
        • N NavnathKale

          try setting buffer length to less as possible but moderate enough. I usually use 512KB or 1MB buffer length for large file handling. Hope this will help you :)

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

          How would the size of my buffer change the delay when calling the Close method? Wouldn't a smaller/larger buffer size effect the Flush method only?

          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