[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1872
  • Last Modified:

C#.NET - Problems with stream read/write timing glitches

I'm trying to read from a Stream and write to another Stream. The output stream is a disk file, whereas the input stream is one provided by SharpZip (http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx).

I've put a BinaryReader on the SharpZip Stream and a BinaryWriter on the disk file's handle.

Whenever the stream data is copied I run into glitches that result in truncated files, 0-byte files, files that are closed before they've started to be written, etc. In general it appears that BinaryWriter is executing in a different, slower thread than the read operations. However, I haven't put any multithreading code into my app.

Here is applicable code:
                      Outfile = new FileStream((UT2004Location + en.Value.ToString()), ((bOverwrite) ? (FileMode.Create) : (FileMode.CreateNew)), FileAccess.Write);
                      Output = new BinaryWriter(Outfile);
                      // Read in data
                      Instream = Infile.GetInputStream(Infile.GetEntry(en.Key.ToString()));
                      Input = new BinaryReader(Instream);
                      Size = 2048;
                      while (true)
                      {
                          Size = Input.Read(Buffer, 0, Buffer.Length);
                          if (Size > 0)
                          {
                              Output.Write(Buffer);
                          }
                          else
                          {
                              break;
                          }
                      }
                    Input.Close();
                    Instream.Close();
                    Output.Close();
                    Outfile.Close();

Thanks in advance,

mtyrce
0
mtyrce
Asked:
mtyrce
1 Solution
 
apeterCommented:
Hi,
   Maybe SharpZip is doing some multi threading..check it out is functionality

-Aaron
0
 
ozymandiasCommented:
When you wroite to your BinaryStream, try doing this :

        Output.Write(Buffer,0,Size);

instead of this :

        Output.Write(Buffer);

0
 
mtyrceAuthor Commented:
Thanks for the tips everyone,

I figured it out. It looks like it was two things:

1. I didn't Output.Flush() before Output.Close(), and
2. I shouldn't have had Input.Close() and Instream.Close() (those gave me errors that the next file could not be read since it was already closed.)

Sorry for wasting everyone's time, I'm a .NET newbie. :(

Going to request a refund in Community Service.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
ozymandiasCommented:
I have no problem with that.
However, I would still recommend the change I suggested.

Cheers.
0
 
mtyrceAuthor Commented:
ozymanidias, that was what the line was before; I messed with it during debugging attempts. I'll change it back if it's better for some reason.
0
 
LunchyCommented:
Closed, 400 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now