异步/的await - 当返回任务VS无效? [英] async/await - when to return a Task vs void?

查看:347
本文介绍了异步/的await - 当返回任务VS无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么样的情况下总会有想使用

 公共异步任务AsyncMethod(INT NUM)

而不是

 公共异步无效AsyncMethod(INT NUM)

这是我能想到的唯一情况是,如果你需要的任务能够跟踪它的进展情况。

此外,在下面的方法中,是异步和等待关键字不必要

 公共静态异步无效AsyncMethod2(INT NUM)
    {
        等待Task.Factory.StartNew(()=> Thread.sleep代码(NUM));
    }


解决方案

1)通常情况下,你会想要返回工作。当你的需求的有无效收益类型(事件)的主要例外应该的。如果没有理由禁止具有主叫伺机你的任务,为什么不允许呢?

2)异步方法,返回无效在另一个方面特别:他们重新present的顶级异步操作的,并且有开始发挥作用时,你的任务返回异常的附加规则。最简单的方法就是以示区别是一个例子:

 静态异步无效F()
{
    等待H();
}静态异步任务克()
{
    等待H();
}静态异步任务H()
{
    抛出新NotImplementedException();
}私人无效的button1_Click(对象发件人,EventArgs的发送)
{
    F();
}私人无效button2_Click(对象发件人,EventArgs的发送)
{
    G();
}私人无效button3_Click(对象发件人,EventArgs的发送)
{
    所以GC.Collect();
}

˚F的例外,始终是观察。留下一个顶级异步方法异常简单地像对待其他任何未处理的异常。 先按g 的异常从未观察到。当垃圾收集器来清理任务,它看到任务导致了异常,也没有人处理的异常。当这种情况发生时, TaskScheduler.UnobservedTaskException 处理程序运行。你永远不应该让这种情况发生。要使用你的榜样,

 公共静态异步无效AsyncMethod2(INT NUM)
{
    等待Task.Factory.StartNew(()=> Thread.sleep代码(NUM));
}

是,使用异步伺机在这里,他们要确保你的方法仍然工作正常,如果有异常抛出

有关更多信息,请参见:<一href=\"http://msdn.microsoft.com/en-us/magazine/jj991977.aspx\">http://msdn.microsoft.com/en-us/magazine/jj991977.aspx

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 it's 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: http://msdn.microsoft.com/en-us/magazine/jj991977.aspx

这篇关于异步/的await - 当返回任务VS无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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