views:

71

answers:

2

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();
    }
}
+3  A: 

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();
        }
    }
}
Adam
I'm passing a callback to the BeginWrite method and don't get an IAsyncResult object from it. The callback has the signature:private void WriteCallback(IAsyncResult asyncResult) and operates on asyncResult passed to it. (In my Reading pair (BeginRead - ReadCallback it works fine).
Serge
Adam, thank you for the answerI've just tried it - stored the reference returned by the BeginWrite and used it in the callback instead of passed as an argument.The same exception is thrown again.
Serge
`BeginWrite` on a `Stream` returns `IAsyncResult`. However, I forgot the callback signature contained this, so its moot anyway - you don't need to use the returned reference.
Adam
@Serge if this is the case, then you have a logic bug somewhere - though I'd need to see the code for that. Try the sample code I provided, and substitute your own file in.
Adam
ConsoleApplicetion14? That's a lot of default projects in your project folder ;)
Jesse C. Slicer
@Jesse lol that doesn't include all the WinForms ones either!
Adam
+2  A: 

You've got the following:

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

but you are writing. Shouldn't you be calling EndWrite? Or am I missing something really obvious?

There's also a ProgressRead(); in that method too.

ChrisF
Yes, there should be EndWriteThank you, ChrisF
Serge