内存异步数据流的方法:下列哪一项? [英] Asynchronous memory streaming approach: which of the following?

查看:192
本文介绍了内存异步数据流的方法:下列哪一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,它采用异步存储器流媒体解决方案,我想实现这样的正确的方法。哪一个更方便?第一,简单的:

  //第一种方法:线性异步
私人异步静态任务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屋!

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