async/await - 何时返回 Task 与 void? [英] async/await - when to return a Task vs void?

查看:30
本文介绍了async/await - 何时返回 Task 与 void?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么场景下会用到

public async Task AsyncMethod(int num)

代替

public async void AsyncMethod(int num)

我能想到的唯一场景是,如果您需要任务能够跟踪其进度.

另外,在下面的方法中,async和await关键字是不是不需要?

public static async void AsyncMethod2(int num){等待 Task.Factory.StartNew(() => Thread.Sleep(num));}

解决方案

  1. 通常,您会想要返回一个Task.主要的例外应该是当您需要有一个 void 返回类型(对于事件).如果没有理由禁止调用者 await 你的任务,为什么禁止它?

  2. async 返回void 的方法在另一方面是特殊的:它们代表顶级异步操作,并且有额外的规则当您的任务返回异常时,它就会发挥作用.最简单的方法是用一个例子来说明差异:

static async void f(){等待 h();}静态异步任务 g(){等待 h();}静态异步任务 h(){抛出新的 NotImplementedException();}private void button1_Click(object sender, EventArgs e){F();}private void button2_Click(对象发送者,EventArgs e){G();}private void button3_Click(对象发送者,EventArgs e){GC.Collect();}

f 的异常总是被观察到".离开顶级异步方法的异常被简单地视为任何其他未处理的异常.g 的异常从未被观察到.当垃圾收集器来清理任务时,它看到任务导致异常,并且没有人处理异常.当这种情况发生时,TaskScheduler.UnobservedTaskException 处理程序运行.你永远不应该让这种情况发生.使用您的示例,

public static async void AsyncMethod2(int num){等待 Task.Factory.StartNew(() => Thread.Sleep(num));}

是的,在此处使用 asyncawait,它们可确保您的方法在抛出异常时仍能正常工作.

有关详细信息,请参阅:https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming

Under what scenarios would one want to use

public async Task AsyncMethod(int num)

instead of

public async void AsyncMethod(int num)

The only scenario that I can think of is if you need the task to be able to track its progress.

Additionally, in the following method, are the async and await keywords unnecessary?

public static async void AsyncMethod2(int num)
{
    await Task.Factory.StartNew(() => Thread.Sleep(num));
}

解决方案

  1. Normally, you would want to return a Task. The main exception should be when you need to have a void return type (for events). If there's no reason to disallow having the caller await your task, why disallow it?

  2. async methods that return void are special in another aspect: they represent top-level async operations, and have additional rules that come into play when your task returns an exception. The easiest way is to show the difference is with an example:

static async void f()
{
    await h();
}

static async Task g()
{
    await h();
}

static async Task h()
{
    throw new NotImplementedException();
}

private void button1_Click(object sender, EventArgs e)
{
    f();
}

private void button2_Click(object sender, EventArgs e)
{
    g();
}

private void button3_Click(object sender, EventArgs e)
{
    GC.Collect();
}

f's exception is always "observed". An exception that leaves a top-level asynchronous method is simply treated like any other unhandled exception. g's exception is never observed. When the garbage collector comes to clean up the task, it sees that the task resulted in an exception, and nobody handled the exception. When that happens, the TaskScheduler.UnobservedTaskException handler runs. You should never let this happen. To use your example,

public static async void AsyncMethod2(int num)
{
    await Task.Factory.StartNew(() => Thread.Sleep(num));
}

Yes, use async and await here, they make sure your method still works correctly if an exception is thrown.

For more information see: https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming

这篇关于async/await - 何时返回 Task 与 void?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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