FileStream.EndWrite throws an exception (.NET)
I'm trying to implement the progress notification mechanism when copying files. I'm doing this in the following way:
- I create two FileStreams - for reading and writing
- 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.
- 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).
- 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_queueBufferL开发者_JAVA百科ock = 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();
}
}
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();
}
}
}
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.
精彩评论