?
Solved

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

Posted on 2004-08-19
7
Medium Priority
?
1,869 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
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 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