471,310 Members | 1,273 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

File size is larger after beginwrite

Chi
I am a newibe of C# and async programming
thx for your help. I think most likely is my logical problem but i cant
figure it out

using System.IO;
using System.Text;
using System;
class Class1
{
static void Main()
{
Class1 a = new Class1();
a.Run();
Console.ReadLine();

}

void cB(IAsyncResult ar)
{

fsOut.EndWrite(ar);
fsIn.BeginRead(buffer,0,buffer.Length,new AsyncCallback(write),null);
}
void write(IAsyncResult ar)
{
int byteRead=fsIn.EndRead(ar);
if(byteRead==0)
{
fsOut.Write(buffer,0,buffer.Length);
fsOut.Close();
fsIn.Close();
}
else
{
fsOut.BeginWrite(buffer,0,buffer.Length,new AsyncCallback(cB),null);
}

}

public void Run()
{
fsOut = new
FileStream("Result.jpg",FileMode.Create,FileAccess .Write,FileShare.ReadWrite,2048,true);
fsIn= new
FileStream("test.jpg",FileMode.Open,FileAccess.Rea d,FileShare.ReadWrite,2048,true);
fsIn.BeginRead(buffer,0,buffer.Length,new AsyncCallback(write),null);
}
Class1()
{

buffer=new byte[2048];
}
//int LastResult;
private FileStream fsIn;
private FileStream fsOut;
byte [] buffer;


}
Jan 30 '06 #1
1 1740
Chi wrote:
I am a newibe of C# and async programming
thx for your help. I think most likely is my logical problem but i cant
figure it out


Well, it always helps if you describe what the problem is as well as
providing code. However, you certainly *do* have a problem, in that
your call to BeginWrite is assuming that it's managed to read a whole
buffer, instead of writing out bytesRead amount of data.

You're also writing out a full buffer's worth of data even if you
haven't read anything (where you shouldn't be writing anything).

Jon

Jan 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Andrew Clark | last post: by
1 post views Thread by Jeff Weber | last post: by
8 posts views Thread by luis molina Micasoft | last post: by
1 post views Thread by MSDN | last post: by
5 posts views Thread by Jefferis NoSpamme | last post: by
6 posts views Thread by bobby | last post: by
4 posts views Thread by rsaharia | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.