await AsyncMethod() versus await await Task.Factory.StartNew(AsyncMethod)

前端 未结 2 1237
独厮守ぢ
独厮守ぢ 2021-02-06 03:37

Given the following method:

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


        
2条回答
  •  不思量自难忘°
    2021-02-06 03:59

    Yes, there is a difference: in the first form, you have an extra level of Task, which brings absolutely nothing useful.

    The first form is basically equivalent to this:

    Task> task1 = Task.Factory.StartNew>( DoSomethingAsync);
    Task> task2 = await task1;
    var myObject = await task2;
    

    So it doesn't really make sense: you're creating a task that just... creates another task.

提交回复
热议问题