Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 935303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:08:07+00:00 2026-05-15T21:08:07+00:00

I’m trying to implement the progress notification mechanism when copying files. I’m doing this

  • 0

I’m trying to implement the progress notification mechanism when copying files.
I’m doing this in the following way:

  1. I create two FileStreams – for reading and writing
  2. Call the BeginRead passing to it a ReadCallback and a structure containing the reading stream and the array to fill with the data read from a file.
  3. In the ReadCallback I call the EndRead method and call the BeginWrite method on the file stream used for writing passing to it a WriteCallback and a structure containing the stream (used for writing) and the array to write (the same as with the reading part).
  4. When I call the EndWrite in the WriteCallback passing to it the asyncResult it throws an ArgumentException: Either the IAsyncResult object did not come from the corresponding async method on this type, or EndRead was called multiple times with the same IAsyncResult

Please help to resolve this.

Update Source code:

private void StartAsyncCopying()
{
    var sourceFileInfo = new FileInfo(Source);
    if (!sourceFileInfo.Exists)
    {
        throw new FileNotFoundException("File not found.",
            Source);
    }
    m_sourceLength = sourceFileInfo.Length;
    m_readerStream = new FileStream(Source, FileMode.Open);
    m_writerStream = new FileStream(Target, FileMode.Create);
    m_queueBuffer = new Queue<byte[]>(DefaultQueueBufferSize);
    m_queueBufferLock = new object();

    ProgressRead();
}

private void ProgressRead()
{
    var readerChunck = new byte[DefaultChunckSize];
    var streamChunck = new StreamChunck(readerChunck, m_readerStream);
    m_readerStream.BeginRead(streamChunck.Chunck,
        0,
        streamChunck.Chunck.Length,
        ReadCallback,
        streamChunck);
}


private void ReadCallback(IAsyncResult asyncResult)
{
    var streamChunck = asyncResult.AsyncState as StreamChunck;
    var numberOfBytesRead = streamChunck.Stream.EndRead(asyncResult);

    m_readerOffset += numberOfBytesRead;

    ProgressWrite(streamChunck.Chunck);
}

private void ProgressWrite(byte[] chunck)
{
    var streamChunck = new StreamChunck(chunck, m_writerStream);
    m_writerAsyncResult = m_writerStream.BeginWrite(streamChunck.Chunck,
        0,
        streamChunck.Chunck.Length,
        WriteCallback,
        streamChunck);
}

private void WriteCallback(IAsyncResult asyncResult)
{
    var streamChunck = asyncResult.AsyncState as StreamChunck;
    var numberOfBytesWritten = streamChunck.Stream.EndRead(asyncResult);
    m_writerOffset += numberOfBytesWritten;

    var progressChangedEventArgs = new CopyProgressChangedEventArgs(m_operationDescription,
        m_sourceLength,
        m_writerOffset);
    OnCopyProgressChanged(progressChangedEventArgs);

    if (m_writerOffset == m_sourceLength)
    {
        var copyCompletedEventArgs = new CopyCompletedEventArgs(m_operationDescription, null);
        OnCopyCompleted(copyCompletedEventArgs);
    }
    else
    {
        ProgressRead();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:08:08+00:00Added an answer on May 15, 2026 at 9:08 pm

    When you call BeginXXX you are likely given an IAsyncResult object, to call EndXXX you need to pass this IAsyncResult reference into the method. If you have used the same object from the Read and passed that into the Write, it will not work – in your scenario there will be two separate IAsyncResult objects.

    When I do this I keep a reference to the returned IAsyncResult as a local class variable (if using callbacks). When I call EndXXX I provide this local reference (gained from my initial BeginXXX) and null it afterwards to signify it can be reused.

    Update 2: looking at your code, what should be EndWrite in WriteCallback is actually an EndRead, picnic error 🙂

    Update 1: the following works fine for me…

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication14
    {
        class Program
        {
            static FileStream fs = null;
            static MemoryStream ms = null;
            static byte[] buffer = new byte[512];
    
            static void Main(string[] args)
            {
                fs = new FileStream("theFile", FileMode.Open);
                fs.BeginRead(buffer, 0, 512, new AsyncCallback(ReadFinished), null);
    
                Console.Read();
            }
    
            static void ReadFinished(IAsyncResult res)
            {
                fs.EndRead(res);
                fs.Dispose();
    
                ms = new MemoryStream();
                ms.BeginWrite(buffer, 0, 512, new AsyncCallback(WriteFinished), null);
            }
    
            static void WriteFinished(IAsyncResult res)
            {
                ms.EndWrite(res);
                ms.Dispose();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.