对于GZipStream使用以下构造,当GZipStream
是CopyToAsync
的目标时,它似乎永远不会调用我的自定义流的* Async方法。
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
方法?
您可以找到此here
的工作样本 更新:调用初始Write()
方法时调用
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)
答案 0 :(得分:2)
自定义流实施BeginWrite
/ EndWrite
(以及BeginRead
/ EndRead
)更为正确。如果您使用我的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
已添加到1.2.0-alpha-01
的{{1}}版本中。