内存异步数据流的方法:下列哪一项? [英] Asynchronous memory streaming approach: which of the following?
问题描述
我的工作,它采用异步存储器流媒体解决方案,我想实现这样的正确的方法。哪一个更方便?第一,简单的:
//第一种方法:线性异步
私人异步静态任务WriteToStreamFirstVariant()
{
MemoryStream的MemoryStream的=新的MemoryStream();
字节[]数据=新的字节[256];
尝试
{
等待memoryStream.WriteAsync(数据,0,data.Length);
}
赶上(例外的例外)
{
//处理异常
}
最后
{
memoryStream.Dispose();
}
}
或者嵌套任务和关闭第二?
//第二种方法:嵌套任务异步
私人异步静态任务WriteToStreamSecondVariant()
{
等待Task.Run(异步()=>
{
字节[]数据=新的字节[256];
使用(MemoryStream的MemoryStream的=新的MemoryStream())
{
等待memoryStream.WriteAsync(数据,0,data.Length)
.ContinueWith((任务writingTask)= GT;
{
//处理异常
AggregateException例外= writingTask.Exception;
});
}
});
}
事实上,的MemoryStream
的读/ WriteAsync
的方法实际上并不提供任何形式真正异步执行。他们所做的一切是同步进行操作,并返回一个已经完成的工作
。因此,有是调用异步方法没有任何好处,当你知道这是一个的MemoryStream
。事实上,这只是完全不必要的开销。
现在,忘记了一秒钟只是回答你的问题的风格,第一种方式是更好的,因为你不分配/计划新工作
不必要的(例如,使用任务::运行
),但我不知道你为什么会不直接使用使用()
在方法声明。因此,这里的清洁/简单恕我直言:
专用异步静态任务WriteToStreamFirstVariantSimplified()
{
使用(MemoryStream的MemoryStream的=新的MemoryStream())
{
字节[]数据=新的字节[256]; 尝试
{
等待memoryStream.WriteAsync(数据,0,data.Length);
}
赶上(例外的例外)
{
//处理异常
}
}
}
I am working on solution which uses asynchronous memory streaming and I am thinking about right approach for implementing such. Which one is more convenient? The first, simple:
//First approach: linear async
private async static Task WriteToStreamFirstVariant()
{
MemoryStream memoryStream = new MemoryStream();
byte[] data = new byte[256];
try
{
await memoryStream.WriteAsync(data, 0, data.Length);
}
catch(Exception exception)
{
//Handling exception
}
finally
{
memoryStream.Dispose();
}
}
Or the second with nested tasks and closures?
//Second approach: nested tasks async
private async static Task WriteToStreamSecondVariant()
{
await Task.Run(async () =>
{
byte[] data = new byte[256];
using (MemoryStream memoryStream = new MemoryStream())
{
await memoryStream.WriteAsync(data, 0, data.Length)
.ContinueWith((Task writingTask) =>
{
//Handling exceptions
AggregateException exceptions = writingTask.Exception;
});
}
});
}
The fact is that MemoryStream
's Read/WriteAsync
methods don't actually provide any kind of true asynchronous implementation. All they do is perform the operation synchronously and return an already completed Task
. Therefore there is no benefit to calling the async methods when you know it's a MemoryStream
. In fact, it's just completely unnecessary overhead.
Now, forgetting that for a second just to answer your question on style, the first approach is better one because you don't allocate/schedule a new Task
unnecessarily (e.g. with Task::Run
), but I don't know why you wouldn't just use a using()
statement in that approach. So here's the cleanest/simplest IMHO:
private async static Task WriteToStreamFirstVariantSimplified()
{
using(MemoryStream memoryStream = new MemoryStream())
{
byte[] data = new byte[256];
try
{
await memoryStream.WriteAsync(data, 0, data.Length);
}
catch(Exception exception)
{
//Handling exception
}
}
}
这篇关于内存异步数据流的方法:下列哪一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!