Creating an async method in .NET 4.0 that can be used with “await” in .NET 4.5

后端 未结 3 475
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 09:53

I have a .NET project that uses C# in .NET 4.0 and VS2010.

What I would like to do is add some async overloads to my library to make doing async programming easier f

相关标签:
3条回答
  • 2020-12-23 10:06

    The simplest way to do this is to return a Task or a Task<T>. That will be enough.

    However this only makes sense if your method really executes asynchronously.

    I also recommend that you follow the usual pattern of naming them like AbcAsync ("Async" suffix). Your callers will not notice any difference to an async method created with C# 5 (because there is none).

    Tip: Just adding async to the method does nothing. Your method will execute sequentially and return a completed task. Making the method return a task must serve a certain purpose - usually this is done because the method inherently executes asynchronously (like a web-service call or file IO).

    If your method only contains computation but no IO (or only blocking IO) it is usually better not to make it async because you gain nothing doing that. Async methods do not always execute on a separate thread. If that last sentence surprised you, you may want to dig a little into this topic.

    0 讨论(0)
  • 2020-12-23 10:09

    As long as you return a Task that completes somehow (whether in a thread or asynchronously), you would support the async model..

    Having a Task execute asynchronously is another story. If you had access to the async keyword and the API you could simply base your method on async calls to other, already supplied async methods. But in this case, you have to hand-craft your async Tasks.

    There might be better ways to do it, but the most elegant way I can see (and have used) is to utilize System.Threading.Tasks.TaskCompletionSource to construct a task, use Begin/End model of asynchronous methods to execute whatever you need to execute. Then, when you have the result on hand, post it to the previously constructed Task instance using your completion source.

    It will certainly be asynchronous, just not as fancy as the ones in upcoming release.

    Disclaimer: I'm no way near an expert on this. Just made some experiments on.

    0 讨论(0)
  • 2020-12-23 10:22

    As others have stated, you start by having a method return Task or Task<TResult>. This is sufficient to await its result in .NET 4.5.

    To have your method fit in as well as possible with future asynchronous code, follow the guidelines in the Task-based Asynchronous Pattern document (also available on MSDN). It provides naming conventions and parameter recommendations, e.g., for supporting cancellation.

    For the implementation of your method, you have a few choices:

    • If you have existing IAsyncResult-based asynchronous methods, use Task.Factory.FromAsync.
    • If you have another asynchronous system, use TaskCompletionSource<TResult>.
    0 讨论(0)
提交回复
热议问题