CopyToAsync的目标位置时,GZipStream不会调用基础流* Async方法 [英] GZipStream does not call underlying streams *Async methods when destination of CopyToAsync

查看:135
本文介绍了CopyToAsync的目标位置时,GZipStream不会调用基础流* Async方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于GZipStream使用以下构造,当GZipStreamCopyToAsync的目的地时,它似乎从未调用过我的自定义流的* Async方法.

Using the following construct for a GZipStream it never seems to call the *Async method of my custom stream when GZipStream is the destination of CopyToAsync.

using (var fs = new System.IO.FileStream(@"C:\BTR\Source\Assemblies\BTR.Rbl.Evolution.Documents.dll", 
        System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None, 8192, true))
{
    using (var ss = new GZipStream(new MyCustomStream(), CompressionMode.Compress))
    {
        await fs.CopyToAsync(ss);
    }
}

似乎只调用BeginWrite/EndWrite机制.有没有一种方法可以从GZipStream派生而使其调用WriteAsync,这样我的自定义流就不必同时实现WriteAsync方法和BeginWrite/EndWrite方法了?

It seems to only call the BeginWrite/EndWrite mechanisms. Is there a way to derive from GZipStream to make it call WriteAsync instead so that my custom stream doesn't have to implement both the WriteAsync method along with the BeginWrite/EndWrite methods?

您可以在此处

更新:调用初始Write()方法

Update: Callstack when initial Write() method called

SampleStream.Write(buffer, offset, count)
System.IO.Compression.DeflateStream.DoMaintenance(array, offset, count)
System.IO.Compression.DeflateStream.InternalWrite(array, offset, count, isAsync)
System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(msg, replySink)
System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(o)
System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(state)
System.Threading.ExecutionContext.RunInternal(executionContext, callback, state, preserveSyncCtx)
System.Threading.ExecutionContext.Run(executionContext, callback, state, preserveSyncCtx)
System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
(Unmanaged code)

推荐答案

对于您的自定义流来说,实现BeginWrite/EndWrite(以及BeginRead/EndRead)更正确.如果您使用我的 AsyncEx.Tasks库,这并不难:

It would be more correct for your custom stream to implement BeginWrite/EndWrite (and BeginRead/EndRead as well). It's not hard if you use my AsyncEx.Tasks library:

public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count,
    AsyncCallback callback, object state)
{
  var task = WriteAsync(buffer, offset, count);
  return ApmAsyncFactory.ToBegin(task, callback, state);
}

public override void EndWrite(IAsyncResult asyncResult)
{
  ApmAsyncFactory.ToEnd(asyncResult);
}

(ApmAsyncFactory已添加到AsyncEx.Tasks1.2.0-alpha-01版本中.)

(ApmAsyncFactory has been added to the 1.2.0-alpha-01 version of AsyncEx.Tasks).

这篇关于CopyToAsync的目标位置时,GZipStream不会调用基础流* Async方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆