Solved

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

Posted on 2004-08-19
7
1,867 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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