编写自己的异步方法 [英] Write your own async method

查看:25
本文介绍了编写自己的异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何以正确"的方式编写自己的异步方法.

我看过很多像这样解释 async/await 模式的帖子:

http://msdn.microsoft.com/en-us/library/hh191443.aspx

//签名中需要注意的三件事://- 该方法有一个 async 修饰符.//- 返回类型是 Task 或 Task.(请参阅返回类型"部分.)//这里是 Task因为 return 语句返回一个整数.//- 方法名称以Async"结尾.异步任务AccessTheWebAsync(){//您需要添加对 System.Net.Http 的引用来声明客户端.HttpClient 客户端 = 新的 HttpClient();//GetStringAsync 返回一个 Task.这意味着当你等待//任务你会得到一个字符串 (urlContents).任务<字符串>getStringTask = client.GetStringAsync("http://msdn.microsoft.com");//您可以在此处完成不依赖于 GetStringAsync 字符串的工作.做独立工作();//await 运算符挂起 AccessTheWebAsync.//- 在 getStringTask 完成之前,AccessTheWebAsync 无法继续.//- 同时,控制权返回给 AccessTheWebAsync 的调用者.//- 当 getStringTask 完成时,控制在这里恢复.//- await 运算符然后从 getStringTask 检索字符串结果.字符串 urlContents = 等待 getStringTask;//return 语句指定一个整数结果.//任何等待 AccessTheWebAsync 的方法检索长度值.返回 urlContents.Length;}私有无效 DoIndependentWork(){resultsTextBox.Text += "工作中......
";}

这适用于任何已经实现此功能的 .NET 方法,例如

  • System.IO 操作
  • 数据库操作
  • 网络相关操作(下载、上传...)

但是如果我想编写自己的方法需要相当长的时间才能完成,而没有我可以使用的方法,并且上面示例的 DoIndependentWork 方法负载很重,该怎么办?

在这种方法中我可以做到:

  • 字符串操作
  • 计算
  • 处理我自己的对象
  • 聚合、比较、过滤、分组、处理内容
  • 列出操作,添加、删除、应对

我又一次偶然发现了许多帖子,人们只是执行以下操作(再次以上面的例子为例):

异步任务AccessTheWebAsync(){HttpClient 客户端 = 新的 HttpClient();任务<字符串>getStringTask = client.GetStringAsync("http://msdn.microsoft.com");等待 DoIndependentWork();字符串 urlContents = 等待 getStringTask;返回 urlContents.Length;}私人任务 DoIndependentWork(){返回 Task.Run(() => {//字符串操作//计算//处理我自己的对象//聚合、比较、过滤、分组、处理东西//列表操作,添加、删除、应对});}

您可能会注意到变化是 DoIndependentWork 现在返回一个 Task,并且在 AccessTheWebAsync 任务中,该方法获得了一个 await.>

重载操作现在被封装在一个 Task.Run() 中,这就是全部吗?如果仅此而已,那么我唯一需要做的就是为库中的每个方法提供异步方法,如下所示:

公共类FooMagic{公共无效 DoSomeMagic(){//做一些同步魔法...}公共任务 DoSomeMagicAsync(){//做一些异步魔术... ?!?返回 Task.Run(() => { DoSomeMagic(); });}}

如果你能向我解释一下就好了,因为即使是这样一个高票数的问题:如何编写简单的异步方法?只用已有的方法来解释,只是使用 asyn/await 模式像提到的问题的这个评论把它带到了重点:如何编写简单的异步方法?

解决方案

实际答案

您可以使用 TaskCompletionSource,它有一个 Promise Task'不执行任何代码,只执行:

<块引用>

表示未绑定到委托的 Task 的生产者端,通过 Task 属性提供对消费者端的访问."

在开始异步操作时将该任务返回给调用者,并在结束时设置结果(或异常/取消).确保操作真的是异步的就靠你了.

这是Stephen Toub 的AsyncManualResetEvent 实现:

class AsyncManualResetEvent{私有 volatile TaskCompletionSource_tcs = new TaskCompletionSource();公共任务 WaitAsync() { 返回 _tcs.Task;}公共无效集(){_tcs.TrySetResult(真);}公共无效重置(){而(真){var tcs = _tcs;如果 (!tcs.Task.IsCompleted ||Interlocked.CompareExchange(ref _tcs, new TaskCompletionSource(), tcs) == tcs)返回;}}}

背景

使用async-await基本上有两个原因:

  1. 改进的可扩展性:当您有 I/O 密集型工作(或其他固有的异步操作)时,您可以异步调用它,因此您释放调用线程,它同时能够做其他工作.
  2. 卸载:当您有 CPU 密集型工作时,您可以异步调用它,将一个线程的工作转移到另一个线程(主要用于 GUI 线程).

因此大多数 .Net 框架的异步调用都支持开箱即用的 async 并且为了卸载您使用 Task.Run(如你的例子).您真正需要自己实现async的唯一情况是当您创建一个新的异步调用(I/O 或 async 同步构造).

这些情况极为罕见,这就是为什么您通常会找到以下答案

<块引用>

只用现有的方法解释它,并且只使用 async/await 模式"

<小时>

您可以在TaskCompletionSource 的本质中深入了解>

I would like to know how to write your own async methods the "correct" way.

I have seen many many posts explaining the async/await pattern like this:

http://msdn.microsoft.com/en-us/library/hh191443.aspx

// Three things to note in the signature: 
//  - The method has an async modifier.  
//  - The return type is Task or Task<T>. (See "Return Types" section.)
//    Here, it is Task<int> because the return statement returns an integer. 
//  - The method name ends in "Async."
async Task<int> AccessTheWebAsync()
{ 
    // You need to add a reference to System.Net.Http to declare client.
    HttpClient client = new HttpClient();

    // GetStringAsync returns a Task<string>. That means that when you await the 
    // task you'll get a string (urlContents).
    Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

    // You can do work here that doesn't rely on the string from GetStringAsync.
    DoIndependentWork();

    // The await operator suspends AccessTheWebAsync. 
    //  - AccessTheWebAsync can't continue until getStringTask is complete. 
    //  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
    //  - Control resumes here when getStringTask is complete.  
    //  - The await operator then retrieves the string result from getStringTask. 
    string urlContents = await getStringTask;

    // The return statement specifies an integer result. 
    // Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
    return urlContents.Length;
}

private void DoIndependentWork()
{
    resultsTextBox.Text += "Working........
";
}

This works great for any .NET Method that already implements this functionality like

  • System.IO opertions
  • DataBase opertions
  • Network related operations (downloading, uploading...)

But what if I want to write my own method that takes quite some time to complete where there just is no Method I can use and the heavy load is in the DoIndependentWork method of the above example?

In this method I could do:

  • String manipulations
  • Calculations
  • Handling my own objects
  • Aggregating, comparing, filtering, grouping, handling stuff
  • List operations, adding, removing, coping

Again I have stumbled across many many posts where people just do the following (again taking the above example):

async Task<int> AccessTheWebAsync()
{ 
    HttpClient client = new HttpClient();

    Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

    await DoIndependentWork();

    string urlContents = await getStringTask;

    return urlContents.Length;
}

private Task DoIndependentWork()
{
    return Task.Run(() => {

        //String manipulations
        //Calculations
        //Handling my own objects
        //Aggregating, comparing, filtering, grouping, handling stuff
        //List operations, adding, removing, coping
    });
}

You may notice that the changes are that DoIndependentWork now returns a Task and in the AccessTheWebAsync task the method got an await.

The heavy load operations are now capsulated inside a Task.Run(), is this all it takes? If that's all it takes is the only thing I need to do to provide async Method for every single method in my library the following:

public class FooMagic
{
    public void DoSomeMagic()
    {
        //Do some synchron magic...
    }

    public Task DoSomeMagicAsync()
    {
        //Do some async magic... ?!?
        return Task.Run(() => { DoSomeMagic(); });
    }
}

Would be nice if you could explain it to me since even a high voted question like this: How to write simple async method? only explains it with already existing methods and just using asyn/await pattern like this comment of the mentioned question brings it to the point: How to write simple async method?

解决方案

Actual Answer

You do that using TaskCompletionSource, which has a Promise Task that doesn't execute any code and only:

"Represents the producer side of a Task unbound to a delegate, providing access to the consumer side through the Task property."

You return that task to the caller when you start the asynchronous operation and you set the result (or exception/cancellation) when you end it. Making sure the operation is really asynchronous is on you.

Here is a good example of this kind of root of all async method in Stephen Toub's AsyncManualResetEvent implementation:

class AsyncManualResetEvent 
{ 
    private volatile TaskCompletionSource<bool> _tcs = new TaskCompletionSource<bool>();

    public Task WaitAsync() { return _tcs.Task; } 
    public void Set() { _tcs.TrySetResult(true); } 
    public void Reset() 
    { 
        while (true) 
        { 
            var tcs = _tcs; 
            if (!tcs.Task.IsCompleted || 
                Interlocked.CompareExchange(ref _tcs, new TaskCompletionSource<bool>(), tcs) == tcs) 
                return; 
        } 
    } 
}

Background

There are basically two reasons to use async-await:

  1. Improved scalability: When you have I/O intensive work (or other inherently asynchronous operations), you can call it asynchronously and so you release the calling thread and it's capable of doing other work in the mean time.
  2. Offloading: When you have CPU intensive work, you can call it asynchronously, which moves the work off of one thread to another (mostly used for GUI threads).

So most of the .Net framework's asynchronous calls support async out of the box and for offloading you use Task.Run (as in your example). The only case where you actually need to implement async yourself is when you create a new asynchronous call (I/O or async synchronization constructs for example).

These cases are extremely rare, which is why you mostly find answers that

"Only explains it with already existing methods and just using async/await pattern"


You can go deeper in The Nature of TaskCompletionSource

这篇关于编写自己的异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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