等待AsyncMethod()与的await的await Task.Factory.StartNew< TResult>(AsyncMethod) [英] await AsyncMethod() versus await await Task.Factory.StartNew<TResult>(AsyncMethod)

查看:413
本文介绍了等待AsyncMethod()与的await的await Task.Factory.StartNew< TResult>(AsyncMethod)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于下列方法:

public async Task<MyObject> DoSomethingAsync() {
    // do some work
    await OpenSomeFileAsync();
    return new MyObject();
}

有没有之间的差异:

Is there a difference between:

public async void SomeEventHandler(EventArgs args) {
    var myObject = await await Task.Factory.StartNew<Task<MyObject>>( DoSomethingAsync);
    // do something with myObject
}

public async void SomeEventHandler(EventArgs args) {
    var myObject = await DoSomethingAsync();
    // do something with myObject
}

我在想,DoSomethingAsync的做一些工作的一部分会立即出现在第一种情况下的新任务,但说实话,我真的不明白如何完全任务,异步伺机在工作,我M pretty肯定,我只是过于复杂的东西为我自己。

I was thinking that the "do some work" part of DoSomethingAsync would happen immediately in a new task in the first case, but to be honest I don't really understand fully how Tasks, async and await are working, and I'm pretty sure I'm just overcomplicating things for myself.

编辑:

这问题就来了有关从看这个地铁例如:
<一href=\"http://$c$c.msdn.microsoft.com/windowsapps/Sharing-Content-Target-App-e2689782\">http://$c$c.msdn.microsoft.com/windowsapps/Sharing-Content-Target-App-e2689782

This question came about from looking at this Metro example: http://code.msdn.microsoft.com/windowsapps/Sharing-Content-Target-App-e2689782

具体到MainPage.xaml.cs中,他们有这样的:

Specifically in MainPage.xaml.cs, they have this:

var unused = Task.Factory.StartNew(async () => { // some work... });
// unused is of type Task<TResult>

我试图返工不使用匿名异步功能,我开始想,为什么不只是写异步方法并等待它,而不是调用StartNew和移交在一个异步函数?

I was trying to rework it without using an anonymous async function and I started wondering, why not just write an async method and await it, instead of calling StartNew and handing in an async function?

推荐答案

在大多数情况下,增加一个工作是没有用的,但在某些情况下,它可以是

Most of the time, adding another Task is not useful, but in some cases, it can be.

所不同的是,如果你是在UI线程(或类似的东西)上,并执行 DoSomethingAsync()直接,它的第一部分( / /做了一些工作)也将执行在UI线程上,并因此将方法的任何延续部分(除非他们使用 ConfigureAwait()) 。在另一方面,如果你开始另一个任务,第一部分和中的任何以下部分DoSomethingAsync()将执行对线程池

The difference is if you're on the UI thread (or something similar) and execute DoSomethingAsync() directly, its first part (// do some work) will also execute on the UI thread, and so will any continuation parts of the method (unless they use ConfigureAwait()). On the other hand, if you start another Task, both the first part and any following parts of DoSomethingAsync() will execute on the ThreadPool.

如果 DoSomethingAsync()编写正确,又增加工作不应该给你任何好处(和意志给你更多的开销的缺点),但我能想象有些情况下会有所作为。

If DoSomethingAsync() is written correctly, adding another Task shouldn't give you any advantages (and will give you the disadvantage of more overhead), but I can imagine there are cases where it will make a difference.

此外,而不是使用 Task.Factory.StartNew()和两个等待 S,你可以写:

Also, instead of using Task.Factory.StartNew() and two awaits, you could write:

await Task.Run(DoSomethingAsync);

这篇关于等待AsyncMethod()与的await的await Task.Factory.StartNew&LT; TResult&GT;(AsyncMethod)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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