Solved

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

Posted on 2004-08-19
7
1,861 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Management Instrument 6 59
Change data in datatable 8 16
Loop through Multiple Processes Async 2 17
XML & .net 5 19
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.   …
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)…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now