FileStream.EndWrite 引发异常 (.NET)

发布于 2024-09-10 18:34:10 字数 2702 浏览 2 评论 0原文

我正在尝试在复制文件时实现进度通知机制。 我按以下方式执行此操作:

  1. 我创建两个 FileStreams - 用于读取和写入
  2. 调用 BeginRead 向其传递一个 ReadCallback 以及一个包含读取流和数组来填充从文件中读取的数据。
  3. ReadCallback 中,我调用 EndRead 方法,并在用于写入的文件流上调用 BeginWrite 方法,并将其传递给 WriteCallback< /strong> 和一个包含流(用于写入)和要写入的数组(与读取部分相同)的结构。
  4. 当我在 WriteCallback 中调用 EndWrite 并将 asyncResult 传递给它时,它会抛出一个 ArgumentException: 要么 IAsyncResult 对象不是来自此类型的相应异步方法,或 EndRead 使用相同的 IAsyncResult 被多次调用

请帮助解决此问题。

更新源代码:

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-09-17 18:34:10

当您调用 BeginXXX 时,您可能会得到一个 IAsyncResult 对象,要调用 EndXXX,您需要传递此 IAsyncResult 引用进入方法。如果您在读取中使用了相同的对象并将其传递到写入中,则它将不起作用 - 在您的场景中将有两个单独的 IAsyncResult 对象。

当我这样做时,我将对返回的 IAsyncResult 的引用保留为本地类变量(如果使用回调)。当我调用 EndXXX 时,我提供此本地引用(从我最初的 BeginXXX 获得),然后将其置空以表示它可以重用。

更新2:看看你的代码,WriteCallback中应该是EndWrite的实际上是一个EndRead,野餐错误: -)

更新 1: 以下内容对我来说效果很好......

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();
        }
    }
}

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();
        }
    }
}
别念他 2024-09-17 18:34:10

你有以下内容:

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

但你正在写作。您不应该调用 EndWrite 吗?或者我错过了一些非常明显的东西?

该方法中还有一个 ProgressRead();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文