File size is larger after beginwrite

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chi

    #1

    File size is larger after beginwrite

    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.ReadLin e();

    }

    void cB(IAsyncResult ar)
    {

    fsOut.EndWrite( ar);
    fsIn.BeginRead( buffer,0,buffer .Length,new AsyncCallback(w rite),null);


    }
    void write(IAsyncRes ult ar)
    {
    int byteRead=fsIn.E ndRead(ar);
    if(byteRead==0)
    {
    fsOut.Write(buf fer,0,buffer.Le ngth);
    fsOut.Close();
    fsIn.Close();
    }
    else
    {
    fsOut.BeginWrit e(buffer,0,buff er.Length,new AsyncCallback(c B),null);
    }

    }

    public void Run()
    {
    fsOut = new
    FileStream("Res ult.jpg",FileMo de.Create,FileA ccess.Write,Fil eShare.ReadWrit e,2048,true);
    fsIn= new
    FileStream("tes t.jpg",FileMode .Open,FileAcces s.Read,FileShar e.ReadWrite,204 8,true);
    fsIn.BeginRead( buffer,0,buffer .Length,new AsyncCallback(w rite),null);


    }
    Class1()
    {

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




    }
  • Jon Skeet [C# MVP]

    #2
    Re: File size is larger after beginwrite

    Chi wrote:[color=blue]
    > 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[/color]

    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

    Comment

    Working...