Solved

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

Posted on 2004-08-19
7
1,866 Views
Last Modified: 2010-08-05
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
Comment
Question by:mtyrce
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 11850794
Hi,
   Maybe SharpZip is doing some multi threading..check it out is functionality

-Aaron
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 11851140
When you wroite to your BinaryStream, try doing this :

        Output.Write(Buffer,0,Size);

instead of this :

        Output.Write(Buffer);

0
 

Author Comment

by:mtyrce
ID: 11854166
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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 15

Expert Comment

by:ozymandias
ID: 11855422
I have no problem with that.
However, I would still recommend the change I suggested.

Cheers.
0
 

Author Comment

by:mtyrce
ID: 11866379
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
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 11885957
Closed, 400 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question